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"
37 #define COMMENTS "#;\n"
38 #define NEWLINES "\n\r"
40 typedef enum RunlevelType {
49 const RunlevelType type;
51 /* Standard SysV runlevels */
52 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
53 { "rc1.d", SPECIAL_RESCUE_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_REBOOT_TARGET, RUNLEVEL_DOWN },
60 /* SUSE style boot.d */
61 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
63 /* Debian style rcS.d */
64 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
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_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_MAINTENANCE] = UNIT_MAINTENANCE,
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 int service_set_main_pid(Service *s, pid_t pid) {
139 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
140 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
141 s->meta.id, (unsigned long) pid);
144 s->main_pid_known = true;
146 exec_status_start(&s->main_exec_status, pid);
151 static void service_close_socket_fd(Service *s) {
154 if (s->socket_fd < 0)
157 close_nointr_nofail(s->socket_fd);
161 static void service_connection_unref(Service *s) {
167 socket_connection_unref(s->socket);
171 static void service_done(Unit *u) {
172 Service *s = SERVICE(u);
182 free(s->sysv_runlevels);
183 s->sysv_runlevels = NULL;
185 free(s->status_text);
186 s->status_text = NULL;
188 exec_context_done(&s->exec_context);
189 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
190 s->control_command = NULL;
192 /* This will leak a process, but at least no memory or any of
194 service_unwatch_main_pid(s);
195 service_unwatch_control_pid(s);
198 unit_unwatch_bus_name(UNIT(u), s->bus_name);
203 service_close_socket_fd(s);
204 service_connection_unref(s);
206 unit_unwatch_timer(u, &s->timer_watch);
209 static char *sysv_translate_name(const char *name) {
212 if (!(r = new(char, strlen(name) + sizeof(".service"))))
215 if (startswith(name, "boot."))
216 /* Drop SuSE-style boot. prefix */
217 strcpy(stpcpy(r, name + 5), ".service");
218 else if (endswith(name, ".sh"))
219 /* Drop Debian-style .sh suffix */
220 strcpy(stpcpy(r, name) - 3, ".service");
222 /* Normal init scripts */
223 strcpy(stpcpy(r, name), ".service");
228 static int sysv_translate_facility(const char *name, char **_r) {
230 static const char * const table[] = {
231 /* LSB defined facilities */
232 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
233 "$network", SPECIAL_NETWORK_TARGET,
234 "$named", SPECIAL_NSS_LOOKUP_TARGET,
235 "$portmap", SPECIAL_RPCBIND_TARGET,
236 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
237 "$syslog", SPECIAL_SYSLOG_TARGET,
238 "$time", SPECIAL_RTC_SET_TARGET,
240 /* Debian extensions */
241 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
242 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
243 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
249 for (i = 0; i < ELEMENTSOF(table); i += 2)
250 if (streq(table[i], name)) {
251 if (!(r = strdup(table[i+1])))
260 if (!(r = sysv_translate_name(name)))
271 static int sysv_fix_order(Service *s) {
277 if (s->sysv_start_priority < 0)
280 /* For each pair of services where at least one lacks a LSB
281 * header, we use the start priority value to order things. */
283 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
287 t = (Service*) other;
292 if (t->sysv_start_priority < 0)
295 /* If both units have modern headers we don't care
296 * about the priorities */
297 if ((!s->sysv_path || s->sysv_has_lsb) &&
298 (!t->sysv_path || t->sysv_has_lsb))
301 if (t->sysv_start_priority < s->sysv_start_priority)
303 else if (t->sysv_start_priority > s->sysv_start_priority)
308 /* FIXME: Maybe we should compare the name here lexicographically? */
310 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
317 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
320 if (!(c = new0(ExecCommand, 1)))
323 if (!(c->path = strdup(path))) {
328 if (!(c->argv = strv_new(path, arg1, NULL))) {
337 static int sysv_exec_commands(Service *s) {
341 assert(s->sysv_path);
343 if (!(c = exec_command_new(s->sysv_path, "start")))
345 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
347 if (!(c = exec_command_new(s->sysv_path, "stop")))
349 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
351 if (!(c = exec_command_new(s->sysv_path, "reload")))
353 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
358 static int service_load_sysv_path(Service *s, const char *path) {
375 if (!(f = fopen(path, "re"))) {
376 r = errno == ENOENT ? 0 : -errno;
381 if (!(s->sysv_path = strdup(path))) {
387 char l[LINE_MAX], *t;
389 if (!fgets(l, sizeof(l), f)) {
394 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
404 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
406 s->sysv_has_lsb = true;
410 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
416 t += strspn(t, WHITESPACE);
418 if (state == NORMAL) {
420 /* Try to parse Red Hat style chkconfig headers */
422 if (startswith_no_case(t, "chkconfig:")) {
424 char runlevels[16], *k;
428 if (sscanf(t+10, "%15s %i %*i",
430 &start_priority) != 2) {
432 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
436 /* A start priority gathered from the
437 * symlink farms is preferred over the
438 * data from the LSB header. */
439 if (start_priority < 0 || start_priority > 99)
440 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
441 else if (s->sysv_start_priority < 0)
442 s->sysv_start_priority = start_priority;
444 char_array_0(runlevels);
445 k = delete_chars(runlevels, WHITESPACE "-");
450 if (!(d = strdup(k))) {
455 free(s->sysv_runlevels);
456 s->sysv_runlevels = d;
459 } else if (startswith_no_case(t, "description:") &&
460 !u->meta.description) {
462 size_t k = strlen(t);
465 if (t[k-1] == '\\') {
470 if (!(d = strdup(strstrip(t+12)))) {
475 free(u->meta.description);
476 u->meta.description = d;
478 } else if (startswith_no_case(t, "pidfile:")) {
485 if (!path_is_absolute(fn)) {
486 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
490 if (!(fn = strdup(fn))) {
499 } else if (state == DESCRIPTION) {
501 /* Try to parse Red Hat style description
504 size_t k = strlen(t);
512 assert(u->meta.description);
513 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
518 free(u->meta.description);
519 u->meta.description = d;
521 } else if (state == LSB || state == LSB_DESCRIPTION) {
523 if (startswith_no_case(t, "Provides:")) {
529 FOREACH_WORD_QUOTED(w, z, t+9, i) {
532 if (!(n = strndup(w, z))) {
537 r = sysv_translate_facility(n, &m);
546 if (unit_name_to_type(m) == UNIT_SERVICE)
547 r = unit_add_name(u, m);
549 r = unit_add_two_dependencies_by_name_inverse(u, UNIT_AFTER, UNIT_REQUIRES, m, NULL, true);
557 } else if (startswith_no_case(t, "Required-Start:") ||
558 startswith_no_case(t, "Should-Start:") ||
559 startswith_no_case(t, "X-Start-Before:") ||
560 startswith_no_case(t, "X-Start-After:")) {
566 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
569 if (!(n = strndup(w, z))) {
574 r = sysv_translate_facility(n, &m);
583 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
589 } else if (startswith_no_case(t, "Default-Start:")) {
594 k = delete_chars(t+14, WHITESPACE "-");
597 if (!(d = strdup(k))) {
602 free(s->sysv_runlevels);
603 s->sysv_runlevels = d;
606 } else if (startswith_no_case(t, "Description:") &&
607 !u->meta.description) {
610 /* We use the long description only if
611 * no short description is set. */
613 state = LSB_DESCRIPTION;
615 if (!(d = strdup(strstrip(t+12)))) {
620 free(u->meta.description);
621 u->meta.description = d;
623 } else if (startswith_no_case(t, "Short-Description:")) {
628 if (!(d = strdup(strstrip(t+18)))) {
633 u->meta.description = d;
635 } else if (startswith_no_case(t, "X-Interactive:")) {
638 if ((b = parse_boolean(strstrip(t+14))) < 0) {
639 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
644 s->exec_context.std_input = EXEC_INPUT_TTY;
646 s->exec_context.std_input = EXEC_INPUT_NULL;
648 } else if (state == LSB_DESCRIPTION) {
650 if (startswith(l, "#\t") || startswith(l, "# ")) {
653 assert(u->meta.description);
654 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
659 free(u->meta.description);
660 u->meta.description = d;
667 if ((r = sysv_exec_commands(s)) < 0)
670 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
671 /* If there a runlevels configured for this service
672 * but none of the standard ones, then we assume this
673 * is some special kind of service (which might be
674 * needed for early boot) and don't create any links
677 s->meta.default_dependencies = false;
679 /* Don't timeout special services during boot (like fsck) */
683 /* Special setting for all SysV services */
684 s->type = SERVICE_FORKING;
685 s->valid_no_process = true;
686 s->kill_mode = KILL_PROCESS_GROUP;
687 s->restart = SERVICE_ONCE;
688 s->exec_context.std_output = EXEC_OUTPUT_TTY;
690 u->meta.load_state = UNIT_LOADED;
701 static int service_load_sysv_name(Service *s, const char *name) {
707 /* For SysV services we strip the boot. or .sh
708 * prefixes/suffixes. */
709 if (startswith(name, "boot.") ||
710 endswith(name, ".sh.service"))
713 STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
717 if (asprintf(&path, "%s/%s", *p, name) < 0)
720 assert(endswith(path, ".service"));
721 path[strlen(path)-8] = 0;
723 r = service_load_sysv_path(s, path);
725 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
726 /* Try Debian style xxx.sh source'able init scripts */
728 r = service_load_sysv_path(s, path);
733 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
734 /* Try SUSE style boot.xxx init scripts */
736 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
739 path[strlen(path)-8] = 0;
740 r = service_load_sysv_path(s, path);
747 if ((s->meta.load_state != UNIT_STUB))
754 static int service_load_sysv(Service *s) {
761 /* Load service data from SysV init scripts, preferably with
764 if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
767 if ((t = s->meta.id))
768 if ((r = service_load_sysv_name(s, t)) < 0)
771 if (s->meta.load_state == UNIT_STUB)
772 SET_FOREACH(t, s->meta.names, i) {
776 if ((r == service_load_sysv_name(s, t)) < 0)
779 if (s->meta.load_state != UNIT_STUB)
786 static int service_add_bus_name(Service *s) {
793 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
796 r = unit_merge_by_name(UNIT(s), n);
802 static int service_verify(Service *s) {
805 if (s->meta.load_state != UNIT_LOADED)
808 if (!s->exec_command[SERVICE_EXEC_START]) {
809 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
813 if (s->exec_command[SERVICE_EXEC_START]->command_next) {
814 log_error("%s has more than one ExecStart setting. Refusing.", s->meta.id);
818 if (s->type == SERVICE_DBUS && !s->bus_name) {
819 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
823 if (s->exec_context.pam_name && s->kill_mode != KILL_CONTROL_GROUP) {
824 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
831 static int service_add_default_dependencies(Service *s) {
836 /* Add a number of automatic dependencies useful for the
837 * majority of services. */
839 /* First, pull in base system */
840 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
842 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
845 } else if (s->meta.manager->running_as == MANAGER_SESSION) {
847 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
851 /* Second, activate normal shutdown */
852 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
855 static int service_load(Unit *u) {
857 Service *s = SERVICE(u);
861 /* Load a .service file */
862 if ((r = unit_load_fragment(u)) < 0)
865 /* Load a classic init script as a fallback, if we couldn't find anything */
866 if (u->meta.load_state == UNIT_STUB)
867 if ((r = service_load_sysv(s)) < 0)
870 /* Still nothing found? Then let's give up */
871 if (u->meta.load_state == UNIT_STUB)
874 /* We were able to load something, then let's add in the
875 * dropin directories. */
876 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
879 /* This is a new unit? Then let's add in some extras */
880 if (u->meta.load_state == UNIT_LOADED) {
881 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
884 if ((r = unit_add_default_cgroup(u)) < 0)
887 if ((r = sysv_fix_order(s)) < 0)
891 if ((r = service_add_bus_name(s)) < 0)
894 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
898 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
899 s->notify_access = NOTIFY_MAIN;
901 if (s->type == SERVICE_DBUS || s->bus_name)
902 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
905 if (s->meta.default_dependencies)
906 if ((r = service_add_default_dependencies(s)) < 0)
910 return service_verify(s);
913 static void service_dump(Unit *u, FILE *f, const char *prefix) {
915 ServiceExecCommand c;
916 Service *s = SERVICE(u);
922 p2 = strappend(prefix, "\t");
923 prefix2 = p2 ? p2 : prefix;
926 "%sService State: %s\n"
927 "%sPermissionsStartOnly: %s\n"
928 "%sRootDirectoryStartOnly: %s\n"
929 "%sValidNoProcess: %s\n"
932 "%sNotifyAccess: %s\n",
933 prefix, service_state_to_string(s->state),
934 prefix, yes_no(s->permissions_start_only),
935 prefix, yes_no(s->root_directory_start_only),
936 prefix, yes_no(s->valid_no_process),
937 prefix, kill_mode_to_string(s->kill_mode),
938 prefix, service_type_to_string(s->type),
939 prefix, notify_access_to_string(s->notify_access));
941 if (s->control_pid > 0)
943 "%sControl PID: %lu\n",
944 prefix, (unsigned long) s->control_pid);
949 prefix, (unsigned long) s->main_pid);
954 prefix, s->pid_file);
959 "%sBus Name Good: %s\n",
961 prefix, yes_no(s->bus_name_good));
963 exec_context_dump(&s->exec_context, f, prefix);
965 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
967 if (!s->exec_command[c])
970 fprintf(f, "%s-> %s:\n",
971 prefix, service_exec_command_to_string(c));
973 exec_command_dump_list(s->exec_command[c], f, prefix2);
978 "%sSysV Init Script Path: %s\n"
979 "%sSysV Init Script has LSB Header: %s\n",
980 prefix, s->sysv_path,
981 prefix, yes_no(s->sysv_has_lsb));
983 if (s->sysv_start_priority >= 0)
985 "%sSysVStartPriority: %i\n",
986 prefix, s->sysv_start_priority);
988 if (s->sysv_runlevels)
989 fprintf(f, "%sSysVRunLevels: %s\n",
990 prefix, s->sysv_runlevels);
993 fprintf(f, "%sStatus Text: %s\n",
994 prefix, s->status_text);
999 static int service_load_pid_file(Service *s) {
1006 if (s->main_pid_known)
1009 assert(s->main_pid <= 0);
1014 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1017 r = parse_pid(k, &pid);
1023 if (kill(pid, 0) < 0 && errno != EPERM) {
1024 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1025 (unsigned long) pid, s->pid_file);
1029 if ((r = service_set_main_pid(s, pid)) < 0)
1032 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1033 /* FIXME: we need to do something here */
1039 static int service_get_sockets(Service *s, Set **_set) {
1048 if (s->socket_fd >= 0)
1051 /* Collects all Socket objects that belong to this
1052 * service. Note that a service might have multiple sockets
1053 * via multiple names. */
1055 if (!(set = set_new(NULL, NULL)))
1058 SET_FOREACH(t, s->meta.names, i) {
1062 /* Look for all socket objects that go by any of our
1063 * units and collect their fds */
1065 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1070 p = manager_get_unit(s->meta.manager, k);
1076 if ((r = set_put(set, p)) < 0)
1088 static int service_notify_sockets_dead(Service *s) {
1096 if (s->socket_fd >= 0)
1099 /* Notifies all our sockets when we die */
1100 if ((r = service_get_sockets(s, &set)) < 0)
1103 SET_FOREACH(sock, set, i)
1104 socket_notify_service_dead(sock);
1111 static void service_set_state(Service *s, ServiceState state) {
1112 ServiceState old_state;
1115 old_state = s->state;
1118 if (state != SERVICE_START_PRE &&
1119 state != SERVICE_START &&
1120 state != SERVICE_START_POST &&
1121 state != SERVICE_RELOAD &&
1122 state != SERVICE_STOP &&
1123 state != SERVICE_STOP_SIGTERM &&
1124 state != SERVICE_STOP_SIGKILL &&
1125 state != SERVICE_STOP_POST &&
1126 state != SERVICE_FINAL_SIGTERM &&
1127 state != SERVICE_FINAL_SIGKILL &&
1128 state != SERVICE_AUTO_RESTART)
1129 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1131 if (state != SERVICE_START &&
1132 state != SERVICE_START_POST &&
1133 state != SERVICE_RUNNING &&
1134 state != SERVICE_RELOAD &&
1135 state != SERVICE_STOP &&
1136 state != SERVICE_STOP_SIGTERM &&
1137 state != SERVICE_STOP_SIGKILL)
1138 service_unwatch_main_pid(s);
1140 if (state != SERVICE_START_PRE &&
1141 state != SERVICE_START &&
1142 state != SERVICE_START_POST &&
1143 state != SERVICE_RELOAD &&
1144 state != SERVICE_STOP &&
1145 state != SERVICE_STOP_SIGTERM &&
1146 state != SERVICE_STOP_SIGKILL &&
1147 state != SERVICE_STOP_POST &&
1148 state != SERVICE_FINAL_SIGTERM &&
1149 state != SERVICE_FINAL_SIGKILL) {
1150 service_unwatch_control_pid(s);
1151 s->control_command = NULL;
1152 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1155 if (state == SERVICE_DEAD ||
1156 state == SERVICE_STOP ||
1157 state == SERVICE_STOP_SIGTERM ||
1158 state == SERVICE_STOP_SIGKILL ||
1159 state == SERVICE_STOP_POST ||
1160 state == SERVICE_FINAL_SIGTERM ||
1161 state == SERVICE_FINAL_SIGKILL ||
1162 state == SERVICE_MAINTENANCE ||
1163 state == SERVICE_AUTO_RESTART)
1164 service_notify_sockets_dead(s);
1166 if (state != SERVICE_START_PRE &&
1167 state != SERVICE_START &&
1168 state != SERVICE_START_POST &&
1169 state != SERVICE_RUNNING &&
1170 state != SERVICE_RELOAD &&
1171 state != SERVICE_STOP &&
1172 state != SERVICE_STOP_SIGTERM &&
1173 state != SERVICE_STOP_SIGKILL &&
1174 state != SERVICE_STOP_POST &&
1175 state != SERVICE_FINAL_SIGTERM &&
1176 state != SERVICE_FINAL_SIGKILL &&
1177 !(state == SERVICE_DEAD && s->meta.job)) {
1178 service_close_socket_fd(s);
1179 service_connection_unref(s);
1182 if (old_state != state)
1183 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1185 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1188 static int service_coldplug(Unit *u) {
1189 Service *s = SERVICE(u);
1193 assert(s->state == SERVICE_DEAD);
1195 if (s->deserialized_state != s->state) {
1197 if (s->deserialized_state == SERVICE_START_PRE ||
1198 s->deserialized_state == SERVICE_START ||
1199 s->deserialized_state == SERVICE_START_POST ||
1200 s->deserialized_state == SERVICE_RELOAD ||
1201 s->deserialized_state == SERVICE_STOP ||
1202 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1203 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1204 s->deserialized_state == SERVICE_STOP_POST ||
1205 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1206 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1207 s->deserialized_state == SERVICE_AUTO_RESTART) {
1209 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1212 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1214 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1219 if ((s->deserialized_state == SERVICE_START &&
1220 (s->type == SERVICE_FORKING ||
1221 s->type == SERVICE_DBUS ||
1222 s->type == SERVICE_FINISH ||
1223 s->type == SERVICE_NOTIFY)) ||
1224 s->deserialized_state == SERVICE_START_POST ||
1225 s->deserialized_state == SERVICE_RUNNING ||
1226 s->deserialized_state == SERVICE_RELOAD ||
1227 s->deserialized_state == SERVICE_STOP ||
1228 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1229 s->deserialized_state == SERVICE_STOP_SIGKILL)
1230 if (s->main_pid > 0)
1231 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1234 if (s->deserialized_state == SERVICE_START_PRE ||
1235 s->deserialized_state == SERVICE_START ||
1236 s->deserialized_state == SERVICE_START_POST ||
1237 s->deserialized_state == SERVICE_RELOAD ||
1238 s->deserialized_state == SERVICE_STOP ||
1239 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1240 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1241 s->deserialized_state == SERVICE_STOP_POST ||
1242 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1243 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1244 if (s->control_pid > 0)
1245 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1248 service_set_state(s, s->deserialized_state);
1254 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1258 unsigned rn_fds = 0;
1266 if (s->socket_fd >= 0)
1269 if ((r = service_get_sockets(s, &set)) < 0)
1272 SET_FOREACH(sock, set, i) {
1276 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1288 if (!(t = new(int, rn_fds+cn_fds))) {
1294 memcpy(t, rfds, rn_fds);
1295 memcpy(t+rn_fds, cfds, cn_fds);
1300 rn_fds = rn_fds+cn_fds;
1318 static int service_spawn(
1323 bool apply_permissions,
1325 bool set_notify_socket,
1330 int *fds = NULL, *fdsbuf = NULL;
1332 char **argv = NULL, **env = NULL;
1339 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1340 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1341 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1343 if (s->socket_fd >= 0) {
1344 fds = &s->socket_fd;
1347 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1354 if (timeout && s->timeout_usec) {
1355 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1358 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1360 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1365 if (set_notify_socket) {
1368 if (asprintf(&t, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1373 env = strv_env_set(s->meta.manager->environment, t);
1381 env = s->meta.manager->environment;
1390 s->meta.manager->confirm_spawn,
1391 s->meta.cgroup_bondings,
1397 if (set_notify_socket)
1407 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1408 /* FIXME: we need to do something here */
1420 if (set_notify_socket)
1424 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1429 static int main_pid_good(Service *s) {
1432 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1435 /* If we know the pid file, then lets just check if it is
1437 if (s->main_pid_known)
1438 return s->main_pid > 0;
1440 /* We don't know the pid */
1444 static int control_pid_good(Service *s) {
1447 return s->control_pid > 0;
1450 static int cgroup_good(Service *s) {
1455 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1461 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1468 if (allow_restart &&
1470 (s->restart == SERVICE_RESTART_ALWAYS ||
1471 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1473 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1476 service_set_state(s, SERVICE_AUTO_RESTART);
1478 service_set_state(s, s->failure ? SERVICE_MAINTENANCE : SERVICE_DEAD);
1483 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1484 service_enter_dead(s, false, false);
1487 static void service_enter_signal(Service *s, ServiceState state, bool success);
1489 static void service_enter_stop_post(Service *s, bool success) {
1496 service_unwatch_control_pid(s);
1498 s->control_command_id = SERVICE_EXEC_STOP_POST;
1499 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1500 if ((r = service_spawn(s,
1504 !s->permissions_start_only,
1505 !s->root_directory_start_only,
1507 &s->control_pid)) < 0)
1511 service_set_state(s, SERVICE_STOP_POST);
1513 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1518 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1519 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1522 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1531 if (s->kill_mode != KILL_NONE) {
1532 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1534 if (s->kill_mode == KILL_CONTROL_GROUP) {
1536 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig)) < 0) {
1537 if (r != -EAGAIN && r != -ESRCH)
1546 if (s->main_pid > 0) {
1547 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1553 if (s->control_pid > 0) {
1554 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1565 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1566 if (s->timeout_usec > 0)
1567 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1570 service_set_state(s, state);
1571 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1572 service_enter_stop_post(s, true);
1574 service_enter_dead(s, true, true);
1579 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1581 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1582 service_enter_stop_post(s, false);
1584 service_enter_dead(s, false, true);
1587 static void service_enter_stop(Service *s, bool success) {
1595 service_unwatch_control_pid(s);
1597 s->control_command_id = SERVICE_EXEC_STOP;
1598 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1599 if ((r = service_spawn(s,
1603 !s->permissions_start_only,
1604 !s->root_directory_start_only,
1606 &s->control_pid)) < 0)
1609 service_set_state(s, SERVICE_STOP);
1611 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1616 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1617 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1620 static void service_enter_running(Service *s, bool success) {
1621 int main_pid_ok, cgroup_ok;
1627 main_pid_ok = main_pid_good(s);
1628 cgroup_ok = cgroup_good(s);
1630 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1631 (s->bus_name_good || s->type != SERVICE_DBUS))
1632 service_set_state(s, SERVICE_RUNNING);
1633 else if (s->valid_no_process)
1634 service_set_state(s, SERVICE_EXITED);
1636 service_enter_stop(s, true);
1639 static void service_enter_start_post(Service *s) {
1643 service_unwatch_control_pid(s);
1645 s->control_command_id = SERVICE_EXEC_START_POST;
1646 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1647 if ((r = service_spawn(s,
1651 !s->permissions_start_only,
1652 !s->root_directory_start_only,
1654 &s->control_pid)) < 0)
1657 service_set_state(s, SERVICE_START_POST);
1659 service_enter_running(s, true);
1664 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1665 service_enter_stop(s, false);
1668 static void service_enter_start(Service *s) {
1674 assert(s->exec_command[SERVICE_EXEC_START]);
1675 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1677 if (s->type == SERVICE_FORKING)
1678 service_unwatch_control_pid(s);
1680 service_unwatch_main_pid(s);
1682 if ((r = service_spawn(s,
1683 s->exec_command[SERVICE_EXEC_START],
1684 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1688 s->notify_access != NOTIFY_NONE,
1692 if (s->type == SERVICE_SIMPLE) {
1693 /* For simple services we immediately start
1694 * the START_POST binaries. */
1696 service_set_main_pid(s, pid);
1697 service_enter_start_post(s);
1699 } else if (s->type == SERVICE_FORKING) {
1701 /* For forking services we wait until the start
1702 * process exited. */
1704 s->control_command_id = SERVICE_EXEC_START;
1705 s->control_command = s->exec_command[SERVICE_EXEC_START];
1707 s->control_pid = pid;
1708 service_set_state(s, SERVICE_START);
1710 } else if (s->type == SERVICE_FINISH ||
1711 s->type == SERVICE_DBUS ||
1712 s->type == SERVICE_NOTIFY) {
1714 /* For finishing services we wait until the start
1715 * process exited, too, but it is our main process. */
1717 /* For D-Bus services we know the main pid right away,
1718 * but wait for the bus name to appear on the
1719 * bus. Notify services are similar. */
1721 service_set_main_pid(s, pid);
1722 service_set_state(s, SERVICE_START);
1724 assert_not_reached("Unknown service type");
1729 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1730 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1733 static void service_enter_start_pre(Service *s) {
1738 service_unwatch_control_pid(s);
1740 s->control_command_id = SERVICE_EXEC_START_PRE;
1741 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1742 if ((r = service_spawn(s,
1746 !s->permissions_start_only,
1747 !s->root_directory_start_only,
1749 &s->control_pid)) < 0)
1752 service_set_state(s, SERVICE_START_PRE);
1754 service_enter_start(s);
1759 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1760 service_enter_dead(s, false, true);
1763 static void service_enter_restart(Service *s) {
1767 service_enter_dead(s, true, false);
1769 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1772 log_debug("%s scheduled restart job.", s->meta.id);
1777 log_warning("%s failed to schedule restart job: %s", s->meta.id, strerror(-r));
1778 service_enter_dead(s, false, false);
1781 static void service_enter_reload(Service *s) {
1786 service_unwatch_control_pid(s);
1788 s->control_command_id = SERVICE_EXEC_RELOAD;
1789 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1790 if ((r = service_spawn(s,
1794 !s->permissions_start_only,
1795 !s->root_directory_start_only,
1797 &s->control_pid)) < 0)
1800 service_set_state(s, SERVICE_RELOAD);
1802 service_enter_running(s, true);
1807 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1808 service_enter_stop(s, false);
1811 static void service_run_next(Service *s, bool success) {
1815 assert(s->control_command);
1816 assert(s->control_command->command_next);
1821 s->control_command = s->control_command->command_next;
1823 service_unwatch_control_pid(s);
1825 if ((r = service_spawn(s,
1829 !s->permissions_start_only,
1830 !s->root_directory_start_only,
1832 &s->control_pid)) < 0)
1838 log_warning("%s failed to run next task: %s", s->meta.id, strerror(-r));
1840 if (s->state == SERVICE_START_PRE)
1841 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1842 else if (s->state == SERVICE_STOP)
1843 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1844 else if (s->state == SERVICE_STOP_POST)
1845 service_enter_dead(s, false, true);
1847 service_enter_stop(s, false);
1850 static int service_start(Unit *u) {
1851 Service *s = SERVICE(u);
1855 /* We cannot fulfill this request right now, try again later
1857 if (s->state == SERVICE_STOP ||
1858 s->state == SERVICE_STOP_SIGTERM ||
1859 s->state == SERVICE_STOP_SIGKILL ||
1860 s->state == SERVICE_STOP_POST ||
1861 s->state == SERVICE_FINAL_SIGTERM ||
1862 s->state == SERVICE_FINAL_SIGKILL)
1865 /* Already on it! */
1866 if (s->state == SERVICE_START_PRE ||
1867 s->state == SERVICE_START ||
1868 s->state == SERVICE_START_POST)
1871 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTENANCE || s->state == SERVICE_AUTO_RESTART);
1873 /* Make sure we don't enter a busy loop of some kind. */
1874 if (!ratelimit_test(&s->ratelimit)) {
1875 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1880 s->main_pid_known = false;
1881 s->allow_restart = true;
1883 service_enter_start_pre(s);
1887 static int service_stop(Unit *u) {
1888 Service *s = SERVICE(u);
1892 /* Cannot do this now */
1893 if (s->state == SERVICE_START_PRE ||
1894 s->state == SERVICE_START ||
1895 s->state == SERVICE_START_POST ||
1896 s->state == SERVICE_RELOAD)
1900 if (s->state == SERVICE_STOP ||
1901 s->state == SERVICE_STOP_SIGTERM ||
1902 s->state == SERVICE_STOP_SIGKILL ||
1903 s->state == SERVICE_STOP_POST ||
1904 s->state == SERVICE_FINAL_SIGTERM ||
1905 s->state == SERVICE_FINAL_SIGKILL)
1908 if (s->state == SERVICE_AUTO_RESTART) {
1909 service_set_state(s, SERVICE_DEAD);
1913 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1915 /* This is a user request, so don't do restarts on this
1917 s->allow_restart = false;
1919 service_enter_stop(s, true);
1923 static int service_reload(Unit *u) {
1924 Service *s = SERVICE(u);
1928 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1930 service_enter_reload(s);
1934 static bool service_can_reload(Unit *u) {
1935 Service *s = SERVICE(u);
1939 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1942 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1943 Service *s = SERVICE(u);
1949 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1950 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1952 if (s->control_pid > 0)
1953 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
1955 if (s->main_pid_known && s->main_pid > 0)
1956 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
1958 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1960 /* There's a minor uncleanliness here: if there are multiple
1961 * commands attached here, we will start from the first one
1963 if (s->control_command_id >= 0)
1964 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1966 if (s->socket_fd >= 0) {
1969 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1972 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1975 if (s->main_exec_status.pid > 0) {
1976 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
1978 if (s->main_exec_status.start_timestamp.realtime > 0) {
1979 unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
1980 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
1982 unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
1983 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
1986 if (s->main_exec_status.exit_timestamp.realtime > 0) {
1987 unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
1988 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
1989 unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
1990 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
1992 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
1993 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2000 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2001 Service *s = SERVICE(u);
2009 if (streq(key, "state")) {
2012 if ((state = service_state_from_string(value)) < 0)
2013 log_debug("Failed to parse state value %s", value);
2015 s->deserialized_state = state;
2016 } else if (streq(key, "failure")) {
2019 if ((b = parse_boolean(value)) < 0)
2020 log_debug("Failed to parse failure value %s", value);
2022 s->failure = b || s->failure;
2023 } else if (streq(key, "control-pid")) {
2026 if ((r = parse_pid(value, &pid)) < 0)
2027 log_debug("Failed to parse control-pid value %s", value);
2029 s->control_pid = pid;
2030 } else if (streq(key, "main-pid")) {
2033 if ((r = parse_pid(value, &pid)) < 0)
2034 log_debug("Failed to parse main-pid value %s", value);
2036 service_set_main_pid(s, (pid_t) pid);
2037 } else if (streq(key, "main-pid-known")) {
2040 if ((b = parse_boolean(value)) < 0)
2041 log_debug("Failed to parse main-pid-known value %s", value);
2043 s->main_pid_known = b;
2044 } else if (streq(key, "control-command")) {
2045 ServiceExecCommand id;
2047 if ((id = service_exec_command_from_string(value)) < 0)
2048 log_debug("Failed to parse exec-command value %s", value);
2050 s->control_command_id = id;
2051 s->control_command = s->exec_command[id];
2053 } else if (streq(key, "socket-fd")) {
2056 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2057 log_debug("Failed to parse socket-fd value %s", value);
2060 if (s->socket_fd >= 0)
2061 close_nointr_nofail(s->socket_fd);
2062 s->socket_fd = fdset_remove(fds, fd);
2064 } else if (streq(key, "main-exec-status-pid")) {
2067 if ((r = parse_pid(value, &pid)) < 0)
2068 log_debug("Failed to parse main-exec-status-pid value %s", value);
2070 s->main_exec_status.pid = pid;
2071 } else if (streq(key, "main-exec-status-code")) {
2074 if ((r = safe_atoi(value, &i)) < 0)
2075 log_debug("Failed to parse main-exec-status-code value %s", value);
2077 s->main_exec_status.code = i;
2078 } else if (streq(key, "main-exec-status-status")) {
2081 if ((r = safe_atoi(value, &i)) < 0)
2082 log_debug("Failed to parse main-exec-status-status value %s", value);
2084 s->main_exec_status.status = i;
2085 } else if (streq(key, "main-exec-status-start-realtime")) {
2088 if ((r = safe_atou64(value, &k)) < 0)
2089 log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2091 s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2092 } else if (streq(key, "main-exec-status-start-monotonic")) {
2095 if ((r = safe_atou64(value, &k)) < 0)
2096 log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2098 s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2099 } else if (streq(key, "main-exec-status-exit-realtime")) {
2102 if ((r = safe_atou64(value, &k)) < 0)
2103 log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2105 s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2106 } else if (streq(key, "main-exec-status-exit-monotonic")) {
2109 if ((r = safe_atou64(value, &k)) < 0)
2110 log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2112 s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2114 log_debug("Unknown serialization key '%s'", key);
2119 static UnitActiveState service_active_state(Unit *u) {
2122 return state_translation_table[SERVICE(u)->state];
2125 static const char *service_sub_state_to_string(Unit *u) {
2128 return service_state_to_string(SERVICE(u)->state);
2131 static bool service_check_gc(Unit *u) {
2132 Service *s = SERVICE(u);
2136 return !!s->sysv_path;
2139 static bool service_check_snapshot(Unit *u) {
2140 Service *s = SERVICE(u);
2144 return !s->got_socket_fd;
2147 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2148 Service *s = SERVICE(u);
2154 success = is_clean_exit(code, status);
2155 s->failure = s->failure || !success;
2157 if (s->main_pid == pid) {
2159 exec_status_exit(&s->main_exec_status, pid, code, status);
2162 if (s->type != SERVICE_FORKING) {
2163 assert(s->exec_command[SERVICE_EXEC_START]);
2164 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
2167 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2169 /* The service exited, so the service is officially
2174 case SERVICE_START_POST:
2175 case SERVICE_RELOAD:
2177 /* Need to wait until the operation is
2182 if (s->type == SERVICE_FINISH) {
2183 /* This was our main goal, so let's go on */
2185 service_enter_start_post(s);
2187 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2190 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2195 case SERVICE_RUNNING:
2196 service_enter_running(s, success);
2199 case SERVICE_STOP_SIGTERM:
2200 case SERVICE_STOP_SIGKILL:
2202 if (!control_pid_good(s))
2203 service_enter_stop_post(s, success);
2205 /* If there is still a control process, wait for that first */
2209 assert_not_reached("Uh, main process died at wrong time.");
2212 } else if (s->control_pid == pid) {
2214 if (s->control_command)
2215 exec_status_exit(&s->control_command->exec_status, pid, code, status);
2219 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2221 /* If we are shutting things down anyway we
2222 * don't care about failing commands. */
2224 if (s->control_command && s->control_command->command_next && success) {
2226 /* There is another command to *
2227 * execute, so let's do that. */
2229 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2230 service_run_next(s, success);
2233 /* No further commands for this step, so let's
2234 * figure out what to do next */
2236 s->control_command = NULL;
2237 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2239 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2243 case SERVICE_START_PRE:
2245 service_enter_start(s);
2247 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2251 assert(s->type == SERVICE_FORKING);
2253 /* Let's try to load the pid
2254 * file here if we can. We
2255 * ignore the return value,
2256 * since the PID file might
2257 * actually be created by a
2258 * START_POST script */
2262 service_load_pid_file(s);
2264 service_enter_start_post(s);
2266 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2270 case SERVICE_START_POST:
2271 if (success && s->pid_file && !s->main_pid_known) {
2274 /* Hmm, let's see if we can
2275 * load the pid now after the
2276 * start-post scripts got
2279 if ((r = service_load_pid_file(s)) < 0)
2280 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2285 case SERVICE_RELOAD:
2287 service_enter_running(s, true);
2289 service_enter_stop(s, false);
2294 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2297 case SERVICE_STOP_SIGTERM:
2298 case SERVICE_STOP_SIGKILL:
2299 if (main_pid_good(s) <= 0)
2300 service_enter_stop_post(s, success);
2302 /* If there is still a service
2303 * process around, wait until
2304 * that one quit, too */
2307 case SERVICE_STOP_POST:
2308 case SERVICE_FINAL_SIGTERM:
2309 case SERVICE_FINAL_SIGKILL:
2310 service_enter_dead(s, success, true);
2314 assert_not_reached("Uh, control process died at wrong time.");
2320 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2321 Service *s = SERVICE(u);
2324 assert(elapsed == 1);
2326 assert(w == &s->timer_watch);
2330 case SERVICE_START_PRE:
2332 log_warning("%s operation timed out. Terminating.", u->meta.id);
2333 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2336 case SERVICE_START_POST:
2337 case SERVICE_RELOAD:
2338 log_warning("%s operation timed out. Stopping.", u->meta.id);
2339 service_enter_stop(s, false);
2343 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2344 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2347 case SERVICE_STOP_SIGTERM:
2348 log_warning("%s stopping timed out. Killing.", u->meta.id);
2349 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2352 case SERVICE_STOP_SIGKILL:
2353 /* Uh, wie sent a SIGKILL and it is still not gone?
2354 * Must be something we cannot kill, so let's just be
2355 * weirded out and continue */
2357 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2358 service_enter_stop_post(s, false);
2361 case SERVICE_STOP_POST:
2362 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2363 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2366 case SERVICE_FINAL_SIGTERM:
2367 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2368 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2371 case SERVICE_FINAL_SIGKILL:
2372 log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u->meta.id);
2373 service_enter_dead(s, false, true);
2376 case SERVICE_AUTO_RESTART:
2377 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2378 service_enter_restart(s);
2382 assert_not_reached("Timeout at wrong time.");
2386 static void service_cgroup_notify_event(Unit *u) {
2387 Service *s = SERVICE(u);
2391 log_debug("%s: cgroup is empty", u->meta.id);
2395 /* Waiting for SIGCHLD is usually more interesting,
2396 * because it includes return codes/signals. Which is
2397 * why we ignore the cgroup events for most cases,
2398 * except when we don't know pid which to expect the
2401 case SERVICE_RUNNING:
2402 service_enter_running(s, true);
2410 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2411 Service *s = SERVICE(u);
2416 if (s->notify_access == NOTIFY_NONE) {
2417 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2418 u->meta.id, (unsigned long) pid);
2422 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2423 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2424 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2428 log_debug("%s: Got message", u->meta.id);
2430 /* Interpret MAINPID= */
2431 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2432 (s->state == SERVICE_START ||
2433 s->state == SERVICE_START_POST ||
2434 s->state == SERVICE_RUNNING ||
2435 s->state == SERVICE_RELOAD)) {
2437 if (parse_pid(e + 8, &pid) < 0)
2438 log_warning("Failed to parse %s", e);
2440 log_debug("%s: got %s", u->meta.id, e);
2441 service_set_main_pid(s, pid);
2445 /* Interpret READY= */
2446 if (s->type == SERVICE_NOTIFY &&
2447 s->state == SERVICE_START &&
2448 strv_find(tags, "READY=1")) {
2449 log_debug("%s: got READY=1", u->meta.id);
2451 service_enter_start_post(s);
2454 /* Interpret STATUS= */
2455 if ((e = strv_find_prefix(tags, "STATUS="))) {
2458 if (!(t = strdup(e+7))) {
2459 log_error("Failed to allocate string.");
2463 log_debug("%s: got %s", u->meta.id, e);
2465 free(s->status_text);
2470 static int service_enumerate(Manager *m) {
2474 char *path = NULL, *fpath = NULL, *name = NULL;
2479 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2480 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2485 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2493 if (!(d = opendir(path))) {
2494 if (errno != ENOENT)
2495 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2500 while ((de = readdir(d))) {
2504 if (ignore_file(de->d_name))
2507 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2510 if (strlen(de->d_name) < 4)
2513 a = undecchar(de->d_name[1]);
2514 b = undecchar(de->d_name[2]);
2521 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2526 if (access(fpath, X_OK) < 0) {
2528 if (errno != ENOENT)
2529 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2535 if (!(name = sysv_translate_name(de->d_name + 3))) {
2540 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2541 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2545 if (de->d_name[0] == 'S' &&
2546 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT))
2547 SERVICE(service)->sysv_start_priority =
2548 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2550 manager_dispatch_load_queue(m);
2551 service = unit_follow_merge(service);
2553 if (de->d_name[0] == 'S') {
2555 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2558 } else if (de->d_name[0] == 'K' &&
2559 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2560 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2562 /* We honour K links only for
2563 * halt/reboot. For the normal
2564 * runlevels we assume the
2566 * implicitly added by the
2567 * core logic. Also, we don't
2568 * really distuingish here
2569 * between the runlevels 0 and
2570 * 6 and just add them to the
2571 * special shutdown target. On
2572 * SUSE the boot.d/ runlevel
2573 * is also used for shutdown,
2574 * so we add links for that
2575 * too to the shutdown
2578 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2597 static void service_bus_name_owner_change(
2600 const char *old_owner,
2601 const char *new_owner) {
2603 Service *s = SERVICE(u);
2608 assert(streq(s->bus_name, name));
2609 assert(old_owner || new_owner);
2611 if (old_owner && new_owner)
2612 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2614 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2616 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2618 s->bus_name_good = !!new_owner;
2620 if (s->type == SERVICE_DBUS) {
2622 /* service_enter_running() will figure out what to
2624 if (s->state == SERVICE_RUNNING)
2625 service_enter_running(s, true);
2626 else if (s->state == SERVICE_START && new_owner)
2627 service_enter_start_post(s);
2629 } else if (new_owner &&
2631 (s->state == SERVICE_START ||
2632 s->state == SERVICE_START_POST ||
2633 s->state == SERVICE_RUNNING ||
2634 s->state == SERVICE_RELOAD)) {
2636 /* Try to acquire PID from bus service */
2637 log_debug("Trying to acquire PID from D-Bus name...");
2639 bus_query_pid(u->meta.manager, name);
2643 static void service_bus_query_pid_done(
2648 Service *s = SERVICE(u);
2653 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2655 if (s->main_pid <= 0 &&
2656 (s->state == SERVICE_START ||
2657 s->state == SERVICE_START_POST ||
2658 s->state == SERVICE_RUNNING ||
2659 s->state == SERVICE_RELOAD))
2660 service_set_main_pid(s, pid);
2663 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2667 /* This is called by the socket code when instantiating a new
2668 * service for a stream socket and the socket needs to be
2671 if (s->meta.load_state != UNIT_LOADED)
2674 if (s->socket_fd >= 0)
2677 if (s->state != SERVICE_DEAD)
2681 s->got_socket_fd = true;
2687 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2688 [SERVICE_DEAD] = "dead",
2689 [SERVICE_START_PRE] = "start-pre",
2690 [SERVICE_START] = "start",
2691 [SERVICE_START_POST] = "start-post",
2692 [SERVICE_RUNNING] = "running",
2693 [SERVICE_EXITED] = "exited",
2694 [SERVICE_RELOAD] = "reload",
2695 [SERVICE_STOP] = "stop",
2696 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2697 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2698 [SERVICE_STOP_POST] = "stop-post",
2699 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2700 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2701 [SERVICE_MAINTENANCE] = "maintenance",
2702 [SERVICE_AUTO_RESTART] = "auto-restart",
2705 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2707 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2708 [SERVICE_ONCE] = "once",
2709 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2710 [SERVICE_RESTART_ALWAYS] = "restart-always",
2713 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2715 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2716 [SERVICE_SIMPLE] = "simple",
2717 [SERVICE_FORKING] = "forking",
2718 [SERVICE_FINISH] = "finish",
2719 [SERVICE_DBUS] = "dbus",
2720 [SERVICE_NOTIFY] = "notify"
2723 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2725 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2726 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2727 [SERVICE_EXEC_START] = "ExecStart",
2728 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2729 [SERVICE_EXEC_RELOAD] = "ExecReload",
2730 [SERVICE_EXEC_STOP] = "ExecStop",
2731 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2734 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2736 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2737 [NOTIFY_NONE] = "none",
2738 [NOTIFY_MAIN] = "main",
2739 [NOTIFY_ALL] = "all"
2742 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2744 const UnitVTable service_vtable = {
2745 .suffix = ".service",
2746 .show_status = true,
2748 .init = service_init,
2749 .done = service_done,
2750 .load = service_load,
2752 .coldplug = service_coldplug,
2754 .dump = service_dump,
2756 .start = service_start,
2757 .stop = service_stop,
2758 .reload = service_reload,
2760 .can_reload = service_can_reload,
2762 .serialize = service_serialize,
2763 .deserialize_item = service_deserialize_item,
2765 .active_state = service_active_state,
2766 .sub_state_to_string = service_sub_state_to_string,
2768 .check_gc = service_check_gc,
2769 .check_snapshot = service_check_snapshot,
2771 .sigchld_event = service_sigchld_event,
2772 .timer_event = service_timer_event,
2774 .cgroup_notify_empty = service_cgroup_notify_event,
2775 .notify_message = service_notify_message,
2777 .bus_name_owner_change = service_bus_name_owner_change,
2778 .bus_query_pid_done = service_bus_query_pid_done,
2780 .bus_message_handler = bus_service_message_handler,
2782 .enumerate = service_enumerate