1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU 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 #include "bus-errors.h"
38 #define COMMENTS "#;\n"
39 #define NEWLINES "\n\r"
41 #ifdef HAVE_SYSV_COMPAT
42 typedef enum RunlevelType {
51 const RunlevelType type;
53 /* Standard SysV runlevels for start-up */
54 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
55 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
56 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
57 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
58 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
61 /* SUSE style boot.d */
62 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
66 /* Debian style rcS.d */
67 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
70 /* Standard SysV runlevels for shutdown */
71 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
72 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
74 /* Note that the order here matters, as we read the
75 directories in this order, and we want to make sure that
76 sysv_start_priority is known when we first load the
77 unit. And that value we only know from S links. Hence
78 UP/SYSINIT must be read before DOWN */
81 #define RUNLEVELS_UP "12345"
82 /* #define RUNLEVELS_DOWN "06" */
83 /* #define RUNLEVELS_BOOT "bBsS" */
86 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
87 [SERVICE_DEAD] = UNIT_INACTIVE,
88 [SERVICE_START_PRE] = UNIT_ACTIVATING,
89 [SERVICE_START] = UNIT_ACTIVATING,
90 [SERVICE_START_POST] = UNIT_ACTIVATING,
91 [SERVICE_RUNNING] = UNIT_ACTIVE,
92 [SERVICE_EXITED] = UNIT_ACTIVE,
93 [SERVICE_RELOAD] = UNIT_RELOADING,
94 [SERVICE_STOP] = UNIT_DEACTIVATING,
95 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
96 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
97 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
98 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
99 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
100 [SERVICE_FAILED] = UNIT_FAILED,
101 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
104 static void service_init(Unit *u) {
105 Service *s = SERVICE(u);
108 assert(u->meta.load_state == UNIT_STUB);
110 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
111 s->restart_usec = DEFAULT_RESTART_USEC;
112 s->timer_watch.type = WATCH_INVALID;
113 #ifdef HAVE_SYSV_COMPAT
114 s->sysv_start_priority = -1;
118 exec_context_init(&s->exec_context);
120 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
122 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
125 static void service_unwatch_control_pid(Service *s) {
128 if (s->control_pid <= 0)
131 unit_unwatch_pid(UNIT(s), s->control_pid);
135 static void service_unwatch_main_pid(Service *s) {
138 if (s->main_pid <= 0)
141 unit_unwatch_pid(UNIT(s), s->main_pid);
145 static int service_set_main_pid(Service *s, pid_t pid) {
156 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
157 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
158 s->meta.id, (unsigned long) pid);
161 s->main_pid_known = true;
163 exec_status_start(&s->main_exec_status, pid);
168 static void service_close_socket_fd(Service *s) {
171 if (s->socket_fd < 0)
174 close_nointr_nofail(s->socket_fd);
178 static void service_connection_unref(Service *s) {
184 socket_connection_unref(s->socket);
188 static void service_done(Unit *u) {
189 Service *s = SERVICE(u);
196 #ifdef HAVE_SYSV_COMPAT
200 free(s->sysv_runlevels);
201 s->sysv_runlevels = NULL;
204 free(s->status_text);
205 s->status_text = NULL;
207 exec_context_done(&s->exec_context);
208 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
209 s->control_command = NULL;
211 /* This will leak a process, but at least no memory or any of
213 service_unwatch_main_pid(s);
214 service_unwatch_control_pid(s);
217 unit_unwatch_bus_name(UNIT(u), s->bus_name);
222 service_close_socket_fd(s);
223 service_connection_unref(s);
225 unit_unwatch_timer(u, &s->timer_watch);
228 #ifdef HAVE_SYSV_COMPAT
229 static char *sysv_translate_name(const char *name) {
232 if (!(r = new(char, strlen(name) + sizeof(".service"))))
235 if (startswith(name, "boot."))
236 /* Drop SuSE-style boot. prefix */
237 strcpy(stpcpy(r, name + 5), ".service");
238 else if (endswith(name, ".sh"))
239 /* Drop Debian-style .sh suffix */
240 strcpy(stpcpy(r, name) - 3, ".service");
242 else if (startswith(name, "@"))
243 /* Drop Arch-style background prefix */
244 strcpy(stpcpy(r, name + 1), ".service");
247 /* Normal init scripts */
248 strcpy(stpcpy(r, name), ".service");
253 static int sysv_translate_facility(const char *name, char **_r) {
255 static const char * const table[] = {
256 /* LSB defined facilities */
257 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
258 "$network", SPECIAL_NETWORK_TARGET,
259 "$named", SPECIAL_NSS_LOOKUP_TARGET,
260 "$portmap", SPECIAL_RPCBIND_TARGET,
261 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
262 "$syslog", SPECIAL_SYSLOG_TARGET,
263 "$time", SPECIAL_RTC_SET_TARGET,
265 /* Debian extensions */
267 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
269 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
270 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
273 /* Fedora extensions, lacking the $ prefix */
274 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
275 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
276 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
283 /* SuSE insserv extension */
284 if (streq(name, "$null"))
287 for (i = 0; i < ELEMENTSOF(table); i += 2)
288 if (streq(table[i], name)) {
289 if (!(r = strdup(table[i+1])))
296 r = unit_name_build(name+1, NULL, ".target");
298 r = sysv_translate_name(name);
311 static int sysv_fix_order(Service *s) {
317 if (s->sysv_start_priority < 0)
320 /* For each pair of services where at least one lacks a LSB
321 * header, we use the start priority value to order things. */
323 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
326 bool special_s, special_t;
328 t = (Service*) other;
333 if (t->meta.load_state != UNIT_LOADED)
336 if (t->sysv_start_priority < 0)
339 /* If both units have modern headers we don't care
340 * about the priorities */
341 if ((s->meta.fragment_path || s->sysv_has_lsb) &&
342 (t->meta.fragment_path || t->sysv_has_lsb))
345 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
346 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
348 if (special_t && !special_s)
350 else if (special_s && !special_t)
352 else if (t->sysv_start_priority < s->sysv_start_priority)
354 else if (t->sysv_start_priority > s->sysv_start_priority)
359 /* FIXME: Maybe we should compare the name here lexicographically? */
361 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
368 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
371 if (!(c = new0(ExecCommand, 1)))
374 if (!(c->path = strdup(path))) {
379 if (!(c->argv = strv_new(path, arg1, NULL))) {
388 static int sysv_exec_commands(Service *s) {
392 assert(s->sysv_path);
394 if (!(c = exec_command_new(s->sysv_path, "start")))
396 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
398 if (!(c = exec_command_new(s->sysv_path, "stop")))
400 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
402 if (!(c = exec_command_new(s->sysv_path, "reload")))
404 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
409 static int service_load_sysv_path(Service *s, const char *path) {
426 if (!(f = fopen(path, "re"))) {
427 r = errno == ENOENT ? 0 : -errno;
432 if (!(s->sysv_path = strdup(path))) {
438 char l[LINE_MAX], *t;
440 if (!fgets(l, sizeof(l), f)) {
445 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
455 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
457 s->sysv_has_lsb = true;
461 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
467 t += strspn(t, WHITESPACE);
469 if (state == NORMAL) {
471 /* Try to parse Red Hat style chkconfig headers */
473 if (startswith_no_case(t, "chkconfig:")) {
475 char runlevels[16], *k;
479 if (sscanf(t+10, "%15s %i %*i",
481 &start_priority) != 2) {
483 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
487 /* A start priority gathered from the
488 * symlink farms is preferred over the
489 * data from the LSB header. */
490 if (start_priority < 0 || start_priority > 99)
491 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
492 else if (s->sysv_start_priority < 0)
493 s->sysv_start_priority = start_priority;
495 char_array_0(runlevels);
496 k = delete_chars(runlevels, WHITESPACE "-");
501 if (!(d = strdup(k))) {
506 free(s->sysv_runlevels);
507 s->sysv_runlevels = d;
510 } else if (startswith_no_case(t, "description:") &&
511 !u->meta.description) {
513 size_t k = strlen(t);
516 if (t[k-1] == '\\') {
521 if (!(d = strappend("LSB: ", strstrip(t+12)))) {
526 free(u->meta.description);
527 u->meta.description = d;
529 } else if (startswith_no_case(t, "pidfile:")) {
536 if (!path_is_absolute(fn)) {
537 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
541 if (!(fn = strdup(fn))) {
550 } else if (state == DESCRIPTION) {
552 /* Try to parse Red Hat style description
555 size_t k = strlen(t);
563 assert(u->meta.description);
564 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
569 free(u->meta.description);
570 u->meta.description = d;
572 } else if (state == LSB || state == LSB_DESCRIPTION) {
574 if (startswith_no_case(t, "Provides:")) {
580 FOREACH_WORD_QUOTED(w, z, t+9, i) {
583 if (!(n = strndup(w, z))) {
588 r = sysv_translate_facility(n, &m);
597 if (unit_name_to_type(m) == UNIT_SERVICE)
598 r = unit_add_name(u, m);
600 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
602 if (s->sysv_enabled) {
605 if ((k = unit_add_dependency_by_name_inverse(u, UNIT_WANTS, m, NULL, true)) < 0)
611 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
616 } else if (startswith_no_case(t, "Required-Start:") ||
617 startswith_no_case(t, "Should-Start:") ||
618 startswith_no_case(t, "X-Start-Before:") ||
619 startswith_no_case(t, "X-Start-After:")) {
625 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
628 if (!(n = strndup(w, z))) {
633 r = sysv_translate_facility(n, &m);
642 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
645 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
649 } else if (startswith_no_case(t, "Default-Start:")) {
654 k = delete_chars(t+14, WHITESPACE "-");
657 if (!(d = strdup(k))) {
662 free(s->sysv_runlevels);
663 s->sysv_runlevels = d;
666 } else if (startswith_no_case(t, "Description:") &&
667 !u->meta.description) {
670 /* We use the long description only if
671 * no short description is set. */
673 state = LSB_DESCRIPTION;
675 if (!(d = strappend("LSB: ", strstrip(t+12)))) {
680 free(u->meta.description);
681 u->meta.description = d;
683 } else if (startswith_no_case(t, "Short-Description:")) {
688 if (!(d = strappend("LSB: ", strstrip(t+18)))) {
693 free(u->meta.description);
694 u->meta.description = d;
696 } else if (startswith_no_case(t, "X-Interactive:")) {
699 if ((b = parse_boolean(strstrip(t+14))) < 0) {
700 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
705 s->exec_context.std_input = EXEC_INPUT_TTY;
707 s->exec_context.std_input = EXEC_INPUT_NULL;
709 } else if (state == LSB_DESCRIPTION) {
711 if (startswith(l, "#\t") || startswith(l, "# ")) {
714 assert(u->meta.description);
715 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
720 free(u->meta.description);
721 u->meta.description = d;
728 if ((r = sysv_exec_commands(s)) < 0)
731 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
732 /* If there a runlevels configured for this service
733 * but none of the standard ones, then we assume this
734 * is some special kind of service (which might be
735 * needed for early boot) and don't create any links
738 s->meta.default_dependencies = false;
740 /* Don't timeout special services during boot (like fsck) */
744 /* Special setting for all SysV services */
745 s->type = SERVICE_FORKING;
746 s->remain_after_exit = true;
747 s->restart = SERVICE_ONCE;
748 s->exec_context.std_output =
749 (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY)
750 ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
751 s->exec_context.kill_mode = KILL_PROCESS_GROUP;
753 u->meta.load_state = UNIT_LOADED;
764 static int service_load_sysv_name(Service *s, const char *name) {
770 /* For SysV services we strip the boot. or .sh
771 * prefixes/suffixes. */
772 if (startswith(name, "boot.") ||
773 endswith(name, ".sh.service"))
776 STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
780 if (asprintf(&path, "%s/%s", *p, name) < 0)
783 assert(endswith(path, ".service"));
784 path[strlen(path)-8] = 0;
786 r = service_load_sysv_path(s, path);
788 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
789 /* Try Debian style xxx.sh source'able init scripts */
791 r = service_load_sysv_path(s, path);
796 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
797 /* Try SUSE style boot.xxx init scripts */
799 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
802 path[strlen(path)-8] = 0;
803 r = service_load_sysv_path(s, path);
810 if ((s->meta.load_state != UNIT_STUB))
817 static int service_load_sysv(Service *s) {
824 /* Load service data from SysV init scripts, preferably with
827 if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
830 if ((t = s->meta.id))
831 if ((r = service_load_sysv_name(s, t)) < 0)
834 if (s->meta.load_state == UNIT_STUB)
835 SET_FOREACH(t, s->meta.names, i) {
839 if ((r = service_load_sysv_name(s, t)) < 0)
842 if (s->meta.load_state != UNIT_STUB)
850 static int service_verify(Service *s) {
853 if (s->meta.load_state != UNIT_LOADED)
856 if (!s->exec_command[SERVICE_EXEC_START]) {
857 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
861 if (s->type != SERVICE_ONESHOT &&
862 s->exec_command[SERVICE_EXEC_START]->command_next) {
863 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
867 if (s->type == SERVICE_DBUS && !s->bus_name) {
868 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
872 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
873 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
880 static int service_add_default_dependencies(Service *s) {
885 /* Add a number of automatic dependencies useful for the
886 * majority of services. */
888 /* First, pull in base system */
889 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
891 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
894 } else if (s->meta.manager->running_as == MANAGER_SESSION) {
896 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
900 /* Second, activate normal shutdown */
901 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
904 static int service_load(Unit *u) {
906 Service *s = SERVICE(u);
910 /* Load a .service file */
911 if ((r = unit_load_fragment(u)) < 0)
914 #ifdef HAVE_SYSV_COMPAT
915 /* Load a classic init script as a fallback, if we couldn't find anything */
916 if (u->meta.load_state == UNIT_STUB)
917 if ((r = service_load_sysv(s)) < 0)
921 /* Still nothing found? Then let's give up */
922 if (u->meta.load_state == UNIT_STUB)
925 /* We were able to load something, then let's add in the
926 * dropin directories. */
927 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
930 /* This is a new unit? Then let's add in some extras */
931 if (u->meta.load_state == UNIT_LOADED) {
932 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
935 if ((r = unit_add_default_cgroup(u)) < 0)
938 #ifdef HAVE_SYSV_COMPAT
939 if ((r = sysv_fix_order(s)) < 0)
944 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
947 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
948 s->notify_access = NOTIFY_MAIN;
950 if (s->type == SERVICE_DBUS || s->bus_name)
951 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
954 if (s->meta.default_dependencies)
955 if ((r = service_add_default_dependencies(s)) < 0)
959 return service_verify(s);
962 static void service_dump(Unit *u, FILE *f, const char *prefix) {
964 ServiceExecCommand c;
965 Service *s = SERVICE(u);
971 p2 = strappend(prefix, "\t");
972 prefix2 = p2 ? p2 : prefix;
975 "%sService State: %s\n"
976 "%sPermissionsStartOnly: %s\n"
977 "%sRootDirectoryStartOnly: %s\n"
978 "%sRemainAfterExit: %s\n"
981 "%sNotifyAccess: %s\n",
982 prefix, service_state_to_string(s->state),
983 prefix, yes_no(s->permissions_start_only),
984 prefix, yes_no(s->root_directory_start_only),
985 prefix, yes_no(s->remain_after_exit),
986 prefix, service_type_to_string(s->type),
987 prefix, service_restart_to_string(s->restart),
988 prefix, notify_access_to_string(s->notify_access));
990 if (s->control_pid > 0)
992 "%sControl PID: %lu\n",
993 prefix, (unsigned long) s->control_pid);
998 prefix, (unsigned long) s->main_pid);
1003 prefix, s->pid_file);
1008 "%sBus Name Good: %s\n",
1009 prefix, s->bus_name,
1010 prefix, yes_no(s->bus_name_good));
1012 exec_context_dump(&s->exec_context, f, prefix);
1014 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1016 if (!s->exec_command[c])
1019 fprintf(f, "%s-> %s:\n",
1020 prefix, service_exec_command_to_string(c));
1022 exec_command_dump_list(s->exec_command[c], f, prefix2);
1025 #ifdef HAVE_SYSV_COMPAT
1028 "%sSysV Init Script Path: %s\n"
1029 "%sSysV Init Script has LSB Header: %s\n",
1030 prefix, s->sysv_path,
1031 prefix, yes_no(s->sysv_has_lsb));
1033 if (s->sysv_start_priority >= 0)
1035 "%sSysVStartPriority: %i\n"
1036 "%sSysVEnabled: %s\n",
1037 prefix, s->sysv_start_priority,
1038 prefix, yes_no(s->sysv_enabled));
1040 if (s->sysv_runlevels)
1041 fprintf(f, "%sSysVRunLevels: %s\n",
1042 prefix, s->sysv_runlevels);
1046 fprintf(f, "%sStatus Text: %s\n",
1047 prefix, s->status_text);
1052 static int service_load_pid_file(Service *s) {
1059 if (s->main_pid_known)
1062 assert(s->main_pid <= 0);
1067 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1070 r = parse_pid(k, &pid);
1076 if (kill(pid, 0) < 0 && errno != EPERM) {
1077 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1078 (unsigned long) pid, s->pid_file);
1082 if ((r = service_set_main_pid(s, pid)) < 0)
1085 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1086 /* FIXME: we need to do something here */
1092 static int service_get_sockets(Service *s, Set **_set) {
1101 if (s->socket_fd >= 0)
1104 /* Collects all Socket objects that belong to this
1105 * service. Note that a service might have multiple sockets
1106 * via multiple names. */
1108 if (!(set = set_new(NULL, NULL)))
1111 SET_FOREACH(t, s->meta.names, i) {
1115 /* Look for all socket objects that go by any of our
1116 * units and collect their fds */
1118 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1123 p = manager_get_unit(s->meta.manager, k);
1129 if ((r = set_put(set, p)) < 0)
1141 static int service_notify_sockets_dead(Service *s) {
1149 if (s->socket_fd >= 0)
1152 /* Notifies all our sockets when we die */
1153 if ((r = service_get_sockets(s, &set)) < 0)
1156 SET_FOREACH(sock, set, i)
1157 socket_notify_service_dead(sock);
1164 static void service_set_state(Service *s, ServiceState state) {
1165 ServiceState old_state;
1168 old_state = s->state;
1171 if (state != SERVICE_START_PRE &&
1172 state != SERVICE_START &&
1173 state != SERVICE_START_POST &&
1174 state != SERVICE_RELOAD &&
1175 state != SERVICE_STOP &&
1176 state != SERVICE_STOP_SIGTERM &&
1177 state != SERVICE_STOP_SIGKILL &&
1178 state != SERVICE_STOP_POST &&
1179 state != SERVICE_FINAL_SIGTERM &&
1180 state != SERVICE_FINAL_SIGKILL &&
1181 state != SERVICE_AUTO_RESTART)
1182 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1184 if (state != SERVICE_START &&
1185 state != SERVICE_START_POST &&
1186 state != SERVICE_RUNNING &&
1187 state != SERVICE_RELOAD &&
1188 state != SERVICE_STOP &&
1189 state != SERVICE_STOP_SIGTERM &&
1190 state != SERVICE_STOP_SIGKILL)
1191 service_unwatch_main_pid(s);
1193 if (state != SERVICE_START_PRE &&
1194 state != SERVICE_START &&
1195 state != SERVICE_START_POST &&
1196 state != SERVICE_RELOAD &&
1197 state != SERVICE_STOP &&
1198 state != SERVICE_STOP_SIGTERM &&
1199 state != SERVICE_STOP_SIGKILL &&
1200 state != SERVICE_STOP_POST &&
1201 state != SERVICE_FINAL_SIGTERM &&
1202 state != SERVICE_FINAL_SIGKILL) {
1203 service_unwatch_control_pid(s);
1204 s->control_command = NULL;
1205 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1208 if (state == SERVICE_DEAD ||
1209 state == SERVICE_STOP ||
1210 state == SERVICE_STOP_SIGTERM ||
1211 state == SERVICE_STOP_SIGKILL ||
1212 state == SERVICE_STOP_POST ||
1213 state == SERVICE_FINAL_SIGTERM ||
1214 state == SERVICE_FINAL_SIGKILL ||
1215 state == SERVICE_FAILED ||
1216 state == SERVICE_AUTO_RESTART)
1217 service_notify_sockets_dead(s);
1219 if (state != SERVICE_START_PRE &&
1220 state != SERVICE_START &&
1221 state != SERVICE_START_POST &&
1222 state != SERVICE_RUNNING &&
1223 state != SERVICE_RELOAD &&
1224 state != SERVICE_STOP &&
1225 state != SERVICE_STOP_SIGTERM &&
1226 state != SERVICE_STOP_SIGKILL &&
1227 state != SERVICE_STOP_POST &&
1228 state != SERVICE_FINAL_SIGTERM &&
1229 state != SERVICE_FINAL_SIGKILL &&
1230 !(state == SERVICE_DEAD && s->meta.job)) {
1231 service_close_socket_fd(s);
1232 service_connection_unref(s);
1235 /* For the inactive states unit_notify() will trim the cgroup,
1236 * but for exit we have to do that ourselves... */
1237 if (state == SERVICE_EXITED)
1238 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1240 if (old_state != state)
1241 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1243 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1246 static int service_coldplug(Unit *u) {
1247 Service *s = SERVICE(u);
1251 assert(s->state == SERVICE_DEAD);
1253 if (s->deserialized_state != s->state) {
1255 if (s->deserialized_state == SERVICE_START_PRE ||
1256 s->deserialized_state == SERVICE_START ||
1257 s->deserialized_state == SERVICE_START_POST ||
1258 s->deserialized_state == SERVICE_RELOAD ||
1259 s->deserialized_state == SERVICE_STOP ||
1260 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1261 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1262 s->deserialized_state == SERVICE_STOP_POST ||
1263 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1264 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1265 s->deserialized_state == SERVICE_AUTO_RESTART) {
1267 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1270 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1272 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1277 if ((s->deserialized_state == SERVICE_START &&
1278 (s->type == SERVICE_FORKING ||
1279 s->type == SERVICE_DBUS ||
1280 s->type == SERVICE_ONESHOT ||
1281 s->type == SERVICE_NOTIFY)) ||
1282 s->deserialized_state == SERVICE_START_POST ||
1283 s->deserialized_state == SERVICE_RUNNING ||
1284 s->deserialized_state == SERVICE_RELOAD ||
1285 s->deserialized_state == SERVICE_STOP ||
1286 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1287 s->deserialized_state == SERVICE_STOP_SIGKILL)
1288 if (s->main_pid > 0)
1289 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1292 if (s->deserialized_state == SERVICE_START_PRE ||
1293 s->deserialized_state == SERVICE_START ||
1294 s->deserialized_state == SERVICE_START_POST ||
1295 s->deserialized_state == SERVICE_RELOAD ||
1296 s->deserialized_state == SERVICE_STOP ||
1297 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1298 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1299 s->deserialized_state == SERVICE_STOP_POST ||
1300 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1301 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1302 if (s->control_pid > 0)
1303 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1306 service_set_state(s, s->deserialized_state);
1312 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1316 unsigned rn_fds = 0;
1324 if (s->socket_fd >= 0)
1327 if ((r = service_get_sockets(s, &set)) < 0)
1330 SET_FOREACH(sock, set, i) {
1334 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1346 if (!(t = new(int, rn_fds+cn_fds))) {
1352 memcpy(t, rfds, rn_fds);
1353 memcpy(t+rn_fds, cfds, cn_fds);
1358 rn_fds = rn_fds+cn_fds;
1376 static int service_spawn(
1381 bool apply_permissions,
1383 bool apply_tty_stdin,
1384 bool set_notify_socket,
1389 int *fds = NULL, *fdsbuf = NULL;
1390 unsigned n_fds = 0, n_env = 0;
1391 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1398 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1399 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1400 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1402 if (s->socket_fd >= 0) {
1403 fds = &s->socket_fd;
1406 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1413 if (timeout && s->timeout_usec) {
1414 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1417 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1419 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1424 if (!(our_env = new0(char*, 3))) {
1429 if (set_notify_socket)
1430 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1435 if (s->main_pid > 0)
1436 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1441 if (!(final_env = strv_env_merge(2,
1442 s->meta.manager->environment,
1457 s->meta.manager->confirm_spawn,
1458 s->meta.cgroup_bondings,
1465 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1466 /* FIXME: we need to do something here */
1472 strv_free(final_env);
1482 strv_free(final_env);
1485 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1490 static int main_pid_good(Service *s) {
1493 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1496 /* If we know the pid file, then lets just check if it is
1498 if (s->main_pid_known)
1499 return s->main_pid > 0;
1501 /* We don't know the pid */
1505 static int control_pid_good(Service *s) {
1508 return s->control_pid > 0;
1511 static int cgroup_good(Service *s) {
1516 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1522 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1529 if (allow_restart &&
1530 !s->forbid_restart &&
1531 (s->restart == SERVICE_RESTART_ALWAYS ||
1532 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1534 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1537 service_set_state(s, SERVICE_AUTO_RESTART);
1539 service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD);
1541 s->forbid_restart = false;
1546 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1547 service_enter_dead(s, false, false);
1550 static void service_enter_signal(Service *s, ServiceState state, bool success);
1552 static void service_enter_stop_post(Service *s, bool success) {
1559 service_unwatch_control_pid(s);
1561 s->control_command_id = SERVICE_EXEC_STOP_POST;
1562 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1563 if ((r = service_spawn(s,
1567 !s->permissions_start_only,
1568 !s->root_directory_start_only,
1571 &s->control_pid)) < 0)
1575 service_set_state(s, SERVICE_STOP_POST);
1577 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1582 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1583 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1586 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1588 Set *pid_set = NULL;
1589 bool wait_for_exit = false;
1596 if (s->exec_context.kill_mode != KILL_NONE) {
1597 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1599 if (s->main_pid > 0) {
1600 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1602 s->main_pid, sig) < 0 && errno != ESRCH)
1604 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1606 wait_for_exit = true;
1609 if (s->control_pid > 0) {
1610 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1612 s->control_pid, sig) < 0 && errno != ESRCH)
1614 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1616 wait_for_exit = true;
1619 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1621 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1626 /* Exclude the main/control pids from being killed via the cgroup */
1627 if (s->main_pid > 0)
1628 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1631 if (s->control_pid > 0)
1632 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1635 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
1636 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1637 log_warning("Failed to kill control group: %s", strerror(-r));
1639 wait_for_exit = true;
1645 if (wait_for_exit) {
1646 if (s->timeout_usec > 0)
1647 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1650 service_set_state(s, state);
1651 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1652 service_enter_stop_post(s, true);
1654 service_enter_dead(s, true, true);
1659 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1661 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1662 service_enter_stop_post(s, false);
1664 service_enter_dead(s, false, true);
1670 static void service_enter_stop(Service *s, bool success) {
1678 service_unwatch_control_pid(s);
1680 s->control_command_id = SERVICE_EXEC_STOP;
1681 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1682 if ((r = service_spawn(s,
1686 !s->permissions_start_only,
1687 !s->root_directory_start_only,
1690 &s->control_pid)) < 0)
1693 service_set_state(s, SERVICE_STOP);
1695 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1700 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1701 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1704 static void service_enter_running(Service *s, bool success) {
1705 int main_pid_ok, cgroup_ok;
1711 main_pid_ok = main_pid_good(s);
1712 cgroup_ok = cgroup_good(s);
1714 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1715 (s->bus_name_good || s->type != SERVICE_DBUS))
1716 service_set_state(s, SERVICE_RUNNING);
1717 else if (s->remain_after_exit)
1718 service_set_state(s, SERVICE_EXITED);
1720 service_enter_stop(s, true);
1723 static void service_enter_start_post(Service *s) {
1727 service_unwatch_control_pid(s);
1729 s->control_command_id = SERVICE_EXEC_START_POST;
1730 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1731 if ((r = service_spawn(s,
1735 !s->permissions_start_only,
1736 !s->root_directory_start_only,
1739 &s->control_pid)) < 0)
1742 service_set_state(s, SERVICE_START_POST);
1744 service_enter_running(s, true);
1749 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1750 service_enter_stop(s, false);
1753 static void service_enter_start(Service *s) {
1759 assert(s->exec_command[SERVICE_EXEC_START]);
1760 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1762 if (s->type == SERVICE_FORKING)
1763 service_unwatch_control_pid(s);
1765 service_unwatch_main_pid(s);
1767 s->control_command_id = SERVICE_EXEC_START;
1768 s->control_command = s->exec_command[SERVICE_EXEC_START];
1770 if ((r = service_spawn(s,
1772 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1777 s->notify_access != NOTIFY_NONE,
1781 if (s->type == SERVICE_SIMPLE) {
1782 /* For simple services we immediately start
1783 * the START_POST binaries. */
1785 service_set_main_pid(s, pid);
1786 service_enter_start_post(s);
1788 } else if (s->type == SERVICE_FORKING) {
1790 /* For forking services we wait until the start
1791 * process exited. */
1793 s->control_pid = pid;
1794 service_set_state(s, SERVICE_START);
1796 } else if (s->type == SERVICE_ONESHOT ||
1797 s->type == SERVICE_DBUS ||
1798 s->type == SERVICE_NOTIFY) {
1800 /* For oneshot services we wait until the start
1801 * process exited, too, but it is our main process. */
1803 /* For D-Bus services we know the main pid right away,
1804 * but wait for the bus name to appear on the
1805 * bus. Notify services are similar. */
1807 service_set_main_pid(s, pid);
1808 service_set_state(s, SERVICE_START);
1810 assert_not_reached("Unknown service type");
1815 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1816 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1819 static void service_enter_start_pre(Service *s) {
1824 service_unwatch_control_pid(s);
1826 s->control_command_id = SERVICE_EXEC_START_PRE;
1827 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1828 if ((r = service_spawn(s,
1832 !s->permissions_start_only,
1833 !s->root_directory_start_only,
1836 &s->control_pid)) < 0)
1839 service_set_state(s, SERVICE_START_PRE);
1841 service_enter_start(s);
1846 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1847 service_enter_dead(s, false, true);
1850 static void service_enter_restart(Service *s) {
1855 dbus_error_init(&error);
1857 service_enter_dead(s, true, false);
1859 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
1862 log_debug("%s scheduled restart job.", s->meta.id);
1866 log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
1867 service_enter_dead(s, false, false);
1869 dbus_error_free(&error);
1872 static void service_enter_reload(Service *s) {
1877 service_unwatch_control_pid(s);
1879 s->control_command_id = SERVICE_EXEC_RELOAD;
1880 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1881 if ((r = service_spawn(s,
1885 !s->permissions_start_only,
1886 !s->root_directory_start_only,
1889 &s->control_pid)) < 0)
1892 service_set_state(s, SERVICE_RELOAD);
1894 service_enter_running(s, true);
1899 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1900 service_enter_stop(s, false);
1903 static void service_run_next_control(Service *s, bool success) {
1907 assert(s->control_command);
1908 assert(s->control_command->command_next);
1913 assert(s->control_command_id != SERVICE_EXEC_START);
1915 s->control_command = s->control_command->command_next;
1916 service_unwatch_control_pid(s);
1918 if ((r = service_spawn(s,
1922 !s->permissions_start_only,
1923 !s->root_directory_start_only,
1924 s->control_command_id == SERVICE_EXEC_START_PRE ||
1925 s->control_command_id == SERVICE_EXEC_STOP_POST,
1927 &s->control_pid)) < 0)
1933 log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
1935 if (s->state == SERVICE_START_PRE)
1936 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1937 else if (s->state == SERVICE_STOP)
1938 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1939 else if (s->state == SERVICE_STOP_POST)
1940 service_enter_dead(s, false, true);
1942 service_enter_stop(s, false);
1945 static void service_run_next_main(Service *s, bool success) {
1950 assert(s->control_command);
1951 assert(s->control_command->command_next);
1956 assert(s->control_command_id == SERVICE_EXEC_START);
1957 assert(s->type == SERVICE_ONESHOT);
1959 s->control_command = s->control_command->command_next;
1960 service_unwatch_main_pid(s);
1962 if ((r = service_spawn(s,
1969 s->notify_access != NOTIFY_NONE,
1973 service_set_main_pid(s, pid);
1978 log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
1979 service_enter_stop(s, false);
1982 static int service_start(Unit *u) {
1983 Service *s = SERVICE(u);
1987 /* We cannot fulfill this request right now, try again later
1989 if (s->state == SERVICE_STOP ||
1990 s->state == SERVICE_STOP_SIGTERM ||
1991 s->state == SERVICE_STOP_SIGKILL ||
1992 s->state == SERVICE_STOP_POST ||
1993 s->state == SERVICE_FINAL_SIGTERM ||
1994 s->state == SERVICE_FINAL_SIGKILL)
1997 /* Already on it! */
1998 if (s->state == SERVICE_START_PRE ||
1999 s->state == SERVICE_START ||
2000 s->state == SERVICE_START_POST)
2003 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
2005 /* Make sure we don't enter a busy loop of some kind. */
2006 if (!ratelimit_test(&s->ratelimit)) {
2007 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
2011 if ((s->exec_context.std_input == EXEC_INPUT_SOCKET ||
2012 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
2013 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) &&
2015 log_warning("%s can only be started with a per-connection socket.", u->meta.id);
2020 s->main_pid_known = false;
2021 s->forbid_restart = false;
2023 service_enter_start_pre(s);
2027 static int service_stop(Unit *u) {
2028 Service *s = SERVICE(u);
2032 /* This is a user request, so don't do restarts on this
2034 s->forbid_restart = true;
2037 if (s->state == SERVICE_STOP ||
2038 s->state == SERVICE_STOP_SIGTERM ||
2039 s->state == SERVICE_STOP_SIGKILL ||
2040 s->state == SERVICE_STOP_POST ||
2041 s->state == SERVICE_FINAL_SIGTERM ||
2042 s->state == SERVICE_FINAL_SIGKILL)
2045 /* Don't allow a restart */
2046 if (s->state == SERVICE_AUTO_RESTART) {
2047 service_set_state(s, SERVICE_DEAD);
2051 /* If there's already something running we go directly into
2053 if (s->state == SERVICE_START_PRE ||
2054 s->state == SERVICE_START ||
2055 s->state == SERVICE_START_POST ||
2056 s->state == SERVICE_RELOAD) {
2057 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2061 assert(s->state == SERVICE_RUNNING ||
2062 s->state == SERVICE_EXITED);
2064 service_enter_stop(s, true);
2068 static int service_reload(Unit *u) {
2069 Service *s = SERVICE(u);
2073 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2075 service_enter_reload(s);
2079 static bool service_can_reload(Unit *u) {
2080 Service *s = SERVICE(u);
2084 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2087 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2088 Service *s = SERVICE(u);
2094 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2095 unit_serialize_item(u, f, "failure", yes_no(s->failure));
2097 if (s->control_pid > 0)
2098 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2100 if (s->main_pid_known && s->main_pid > 0)
2101 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2103 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2106 unit_serialize_item(u, f, "status-text", s->status_text);
2108 /* There's a minor uncleanliness here: if there are multiple
2109 * commands attached here, we will start from the first one
2111 if (s->control_command_id >= 0)
2112 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2114 if (s->socket_fd >= 0) {
2117 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2120 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2123 if (s->main_exec_status.pid > 0) {
2124 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2126 if (s->main_exec_status.start_timestamp.realtime > 0) {
2127 unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2128 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2130 unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2131 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2134 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2135 unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2136 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2137 unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2138 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2140 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2141 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2148 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2149 Service *s = SERVICE(u);
2156 if (streq(key, "state")) {
2159 if ((state = service_state_from_string(value)) < 0)
2160 log_debug("Failed to parse state value %s", value);
2162 s->deserialized_state = state;
2163 } else if (streq(key, "failure")) {
2166 if ((b = parse_boolean(value)) < 0)
2167 log_debug("Failed to parse failure value %s", value);
2169 s->failure = b || s->failure;
2170 } else if (streq(key, "control-pid")) {
2173 if (parse_pid(value, &pid) < 0)
2174 log_debug("Failed to parse control-pid value %s", value);
2176 s->control_pid = pid;
2177 } else if (streq(key, "main-pid")) {
2180 if (parse_pid(value, &pid) < 0)
2181 log_debug("Failed to parse main-pid value %s", value);
2183 service_set_main_pid(s, (pid_t) pid);
2184 } else if (streq(key, "main-pid-known")) {
2187 if ((b = parse_boolean(value)) < 0)
2188 log_debug("Failed to parse main-pid-known value %s", value);
2190 s->main_pid_known = b;
2191 } else if (streq(key, "status-text")) {
2194 if ((t = strdup(value))) {
2195 free(s->status_text);
2199 } else if (streq(key, "control-command")) {
2200 ServiceExecCommand id;
2202 if ((id = service_exec_command_from_string(value)) < 0)
2203 log_debug("Failed to parse exec-command value %s", value);
2205 s->control_command_id = id;
2206 s->control_command = s->exec_command[id];
2208 } else if (streq(key, "socket-fd")) {
2211 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2212 log_debug("Failed to parse socket-fd value %s", value);
2215 if (s->socket_fd >= 0)
2216 close_nointr_nofail(s->socket_fd);
2217 s->socket_fd = fdset_remove(fds, fd);
2219 } else if (streq(key, "main-exec-status-pid")) {
2222 if (parse_pid(value, &pid) < 0)
2223 log_debug("Failed to parse main-exec-status-pid value %s", value);
2225 s->main_exec_status.pid = pid;
2226 } else if (streq(key, "main-exec-status-code")) {
2229 if (safe_atoi(value, &i) < 0)
2230 log_debug("Failed to parse main-exec-status-code value %s", value);
2232 s->main_exec_status.code = i;
2233 } else if (streq(key, "main-exec-status-status")) {
2236 if (safe_atoi(value, &i) < 0)
2237 log_debug("Failed to parse main-exec-status-status value %s", value);
2239 s->main_exec_status.status = i;
2240 } else if (streq(key, "main-exec-status-start-realtime")) {
2243 if (safe_atou64(value, &k) < 0)
2244 log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2246 s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2247 } else if (streq(key, "main-exec-status-start-monotonic")) {
2250 if (safe_atou64(value, &k) < 0)
2251 log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2253 s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2254 } else if (streq(key, "main-exec-status-exit-realtime")) {
2257 if (safe_atou64(value, &k) < 0)
2258 log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2260 s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2261 } else if (streq(key, "main-exec-status-exit-monotonic")) {
2264 if (safe_atou64(value, &k) < 0)
2265 log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2267 s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2269 log_debug("Unknown serialization key '%s'", key);
2274 static UnitActiveState service_active_state(Unit *u) {
2277 return state_translation_table[SERVICE(u)->state];
2280 static const char *service_sub_state_to_string(Unit *u) {
2283 return service_state_to_string(SERVICE(u)->state);
2286 #ifdef HAVE_SYSV_COMPAT
2287 static bool service_check_gc(Unit *u) {
2288 Service *s = SERVICE(u);
2292 return !!s->sysv_path;
2296 static bool service_check_snapshot(Unit *u) {
2297 Service *s = SERVICE(u);
2301 return !s->got_socket_fd;
2304 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2305 Service *s = SERVICE(u);
2311 if (!s->meta.fragment_path)
2312 success = is_clean_exit_lsb(code, status);
2314 success = is_clean_exit(code, status);
2316 if (s->main_pid == pid) {
2319 exec_status_exit(&s->main_exec_status, pid, code, status);
2321 if (s->type != SERVICE_FORKING && s->control_command) {
2322 s->control_command->exec_status = s->main_exec_status;
2324 if (s->control_command->ignore)
2328 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2329 "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2330 s->failure = s->failure || !success;
2332 if (s->control_command &&
2333 s->control_command->command_next &&
2336 /* There is another command to *
2337 * execute, so let's do that. */
2339 log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2340 service_run_next_main(s, success);
2344 /* The service exited, so the service is officially
2347 s->control_command = NULL;
2348 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2352 case SERVICE_START_POST:
2353 case SERVICE_RELOAD:
2355 /* Need to wait until the operation is
2360 if (s->type == SERVICE_ONESHOT) {
2361 /* This was our main goal, so let's go on */
2363 service_enter_start_post(s);
2365 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2368 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2373 case SERVICE_RUNNING:
2374 service_enter_running(s, success);
2377 case SERVICE_STOP_SIGTERM:
2378 case SERVICE_STOP_SIGKILL:
2380 if (!control_pid_good(s))
2381 service_enter_stop_post(s, success);
2383 /* If there is still a control process, wait for that first */
2387 assert_not_reached("Uh, main process died at wrong time.");
2391 } else if (s->control_pid == pid) {
2395 if (s->control_command) {
2396 exec_status_exit(&s->control_command->exec_status, pid, code, status);
2398 if (s->control_command->ignore)
2402 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2403 "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2404 s->failure = s->failure || !success;
2406 if (s->control_command &&
2407 s->control_command->command_next &&
2410 /* There is another command to *
2411 * execute, so let's do that. */
2413 log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2414 service_run_next_control(s, success);
2417 /* No further commands for this step, so let's
2418 * figure out what to do next */
2420 s->control_command = NULL;
2421 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2423 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2427 case SERVICE_START_PRE:
2429 service_enter_start(s);
2431 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2435 assert(s->type == SERVICE_FORKING);
2437 /* Let's try to load the pid
2438 * file here if we can. We
2439 * ignore the return value,
2440 * since the PID file might
2441 * actually be created by a
2442 * START_POST script */
2446 service_load_pid_file(s);
2448 service_enter_start_post(s);
2450 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2454 case SERVICE_START_POST:
2455 if (success && s->pid_file && !s->main_pid_known) {
2458 /* Hmm, let's see if we can
2459 * load the pid now after the
2460 * start-post scripts got
2463 if ((r = service_load_pid_file(s)) < 0)
2464 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2469 case SERVICE_RELOAD:
2471 service_enter_running(s, true);
2473 service_enter_stop(s, false);
2478 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2481 case SERVICE_STOP_SIGTERM:
2482 case SERVICE_STOP_SIGKILL:
2483 if (main_pid_good(s) <= 0)
2484 service_enter_stop_post(s, success);
2486 /* If there is still a service
2487 * process around, wait until
2488 * that one quit, too */
2491 case SERVICE_STOP_POST:
2492 case SERVICE_FINAL_SIGTERM:
2493 case SERVICE_FINAL_SIGKILL:
2494 service_enter_dead(s, success, true);
2498 assert_not_reached("Uh, control process died at wrong time.");
2503 /* Notify clients about changed exit status */
2504 unit_add_to_dbus_queue(u);
2507 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2508 Service *s = SERVICE(u);
2511 assert(elapsed == 1);
2513 assert(w == &s->timer_watch);
2517 case SERVICE_START_PRE:
2519 log_warning("%s operation timed out. Terminating.", u->meta.id);
2520 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2523 case SERVICE_START_POST:
2524 case SERVICE_RELOAD:
2525 log_warning("%s operation timed out. Stopping.", u->meta.id);
2526 service_enter_stop(s, false);
2530 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2531 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2534 case SERVICE_STOP_SIGTERM:
2535 log_warning("%s stopping timed out. Killing.", u->meta.id);
2536 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2539 case SERVICE_STOP_SIGKILL:
2540 /* Uh, wie sent a SIGKILL and it is still not gone?
2541 * Must be something we cannot kill, so let's just be
2542 * weirded out and continue */
2544 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2545 service_enter_stop_post(s, false);
2548 case SERVICE_STOP_POST:
2549 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2550 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2553 case SERVICE_FINAL_SIGTERM:
2554 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2555 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2558 case SERVICE_FINAL_SIGKILL:
2559 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
2560 service_enter_dead(s, false, true);
2563 case SERVICE_AUTO_RESTART:
2564 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2565 service_enter_restart(s);
2569 assert_not_reached("Timeout at wrong time.");
2573 static void service_cgroup_notify_event(Unit *u) {
2574 Service *s = SERVICE(u);
2578 log_debug("%s: cgroup is empty", u->meta.id);
2582 /* Waiting for SIGCHLD is usually more interesting,
2583 * because it includes return codes/signals. Which is
2584 * why we ignore the cgroup events for most cases,
2585 * except when we don't know pid which to expect the
2588 case SERVICE_RUNNING:
2589 service_enter_running(s, true);
2592 case SERVICE_STOP_SIGTERM:
2593 case SERVICE_STOP_SIGKILL:
2594 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2595 service_enter_stop_post(s, true);
2599 case SERVICE_FINAL_SIGTERM:
2600 case SERVICE_FINAL_SIGKILL:
2601 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2602 service_enter_dead(s, true, true);
2611 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2612 Service *s = SERVICE(u);
2617 if (s->notify_access == NOTIFY_NONE) {
2618 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2619 u->meta.id, (unsigned long) pid);
2623 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2624 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2625 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2629 log_debug("%s: Got message", u->meta.id);
2631 /* Interpret MAINPID= */
2632 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2633 (s->state == SERVICE_START ||
2634 s->state == SERVICE_START_POST ||
2635 s->state == SERVICE_RUNNING ||
2636 s->state == SERVICE_RELOAD)) {
2638 if (parse_pid(e + 8, &pid) < 0)
2639 log_warning("Failed to parse notification message %s", e);
2641 log_debug("%s: got %s", u->meta.id, e);
2642 service_set_main_pid(s, pid);
2646 /* Interpret READY= */
2647 if (s->type == SERVICE_NOTIFY &&
2648 s->state == SERVICE_START &&
2649 strv_find(tags, "READY=1")) {
2650 log_debug("%s: got READY=1", u->meta.id);
2652 service_enter_start_post(s);
2655 /* Interpret STATUS= */
2656 if ((e = strv_find_prefix(tags, "STATUS="))) {
2660 if (!(t = strdup(e+7))) {
2661 log_error("Failed to allocate string.");
2665 log_debug("%s: got %s", u->meta.id, e);
2667 free(s->status_text);
2670 free(s->status_text);
2671 s->status_text = NULL;
2676 /* Notify clients about changed status or main pid */
2677 unit_add_to_dbus_queue(u);
2680 #ifdef HAVE_SYSV_COMPAT
2681 static int service_enumerate(Manager *m) {
2685 char *path = NULL, *fpath = NULL, *name = NULL;
2686 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
2691 Unit *previous = NULL;
2692 char *arch_daemons = NULL;
2693 char *arch_daemons_stripped = NULL;
2694 char **arch_daemons_split = NULL;
2700 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
2701 "DAEMONS", &arch_daemons,
2705 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
2707 } else if (arch_daemons) {
2708 if (!(arch_daemons_stripped = strchr(arch_daemons, '(')))
2709 arch_daemons_stripped = arch_daemons;
2711 arch_daemons_stripped++; /* strip start paren */
2713 arch_daemons_stripped[strcspn(arch_daemons_stripped, ")")] = 0; /* strip end paren */
2715 if (!(arch_daemons_split = strv_split_quoted(arch_daemons_stripped))) {
2720 STRV_FOREACH(p, arch_daemons_split) {
2724 if (**p == '!') /* daemons prefixed with ! are disabled, so ignore them */
2727 if (!(name = sysv_translate_name(*p))) {
2732 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2733 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2737 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, "multi-user.target", NULL, true)) < 0)
2741 if ((r = unit_add_dependency(service, UNIT_AFTER, previous, true)) < 0)
2744 if (**p != '@') /* daemons prefixed with @ can be started in the background */
2750 zero(runlevel_services);
2752 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2753 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2758 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2766 if (!(d = opendir(path))) {
2767 if (errno != ENOENT)
2768 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2773 while ((de = readdir(d))) {
2776 if (ignore_file(de->d_name))
2779 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2782 if (strlen(de->d_name) < 4)
2785 a = undecchar(de->d_name[1]);
2786 b = undecchar(de->d_name[2]);
2793 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2798 if (access(fpath, X_OK) < 0) {
2800 if (errno != ENOENT)
2801 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2807 if (!(name = sysv_translate_name(de->d_name + 3))) {
2812 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2813 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2817 if (de->d_name[0] == 'S') {
2819 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
2820 SERVICE(service)->sysv_start_priority =
2821 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2823 SERVICE(service)->sysv_enabled = true;
2826 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
2829 if ((r = set_put(runlevel_services[i], service)) < 0)
2832 } else if (de->d_name[0] == 'K' &&
2833 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2834 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2836 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
2839 if ((r = set_put(shutdown_services, service)) < 0)
2845 /* Now we loaded all stubs and are aware of the lowest
2846 start-up priority for all services, not let's actually load
2847 the services, this will also tell us which services are
2848 actually native now */
2849 manager_dispatch_load_queue(m);
2851 /* If this is a native service, rely on native ways to pull in
2852 * a service, don't pull it in via sysv rcN.d links. */
2853 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
2854 SET_FOREACH(service, runlevel_services[i], j) {
2855 service = unit_follow_merge(service);
2857 if (service->meta.fragment_path)
2860 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2864 /* We honour K links only for halt/reboot. For the normal
2865 * runlevels we assume the stop jobs will be implicitly added
2866 * by the core logic. Also, we don't really distuingish here
2867 * between the runlevels 0 and 6 and just add them to the
2868 * special shutdown target. On SUSE the boot.d/ runlevel is
2869 * also used for shutdown, so we add links for that too to the
2870 * shutdown target.*/
2871 SET_FOREACH(service, shutdown_services, j) {
2872 service = unit_follow_merge(service);
2874 if (service->meta.fragment_path)
2877 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2889 free(arch_daemons_split);
2892 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
2893 set_free(runlevel_services[i]);
2894 set_free(shutdown_services);
2903 static void service_bus_name_owner_change(
2906 const char *old_owner,
2907 const char *new_owner) {
2909 Service *s = SERVICE(u);
2914 assert(streq(s->bus_name, name));
2915 assert(old_owner || new_owner);
2917 if (old_owner && new_owner)
2918 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2920 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2922 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2924 s->bus_name_good = !!new_owner;
2926 if (s->type == SERVICE_DBUS) {
2928 /* service_enter_running() will figure out what to
2930 if (s->state == SERVICE_RUNNING)
2931 service_enter_running(s, true);
2932 else if (s->state == SERVICE_START && new_owner)
2933 service_enter_start_post(s);
2935 } else if (new_owner &&
2937 (s->state == SERVICE_START ||
2938 s->state == SERVICE_START_POST ||
2939 s->state == SERVICE_RUNNING ||
2940 s->state == SERVICE_RELOAD)) {
2942 /* Try to acquire PID from bus service */
2943 log_debug("Trying to acquire PID from D-Bus name...");
2945 bus_query_pid(u->meta.manager, name);
2949 static void service_bus_query_pid_done(
2954 Service *s = SERVICE(u);
2959 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2961 if (s->main_pid <= 0 &&
2962 (s->state == SERVICE_START ||
2963 s->state == SERVICE_START_POST ||
2964 s->state == SERVICE_RUNNING ||
2965 s->state == SERVICE_RELOAD))
2966 service_set_main_pid(s, pid);
2969 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2973 /* This is called by the socket code when instantiating a new
2974 * service for a stream socket and the socket needs to be
2977 if (s->meta.load_state != UNIT_LOADED)
2980 if (s->socket_fd >= 0)
2983 if (s->state != SERVICE_DEAD)
2987 s->got_socket_fd = true;
2993 static void service_reset_failed(Unit *u) {
2994 Service *s = SERVICE(u);
2998 if (s->state == SERVICE_FAILED)
2999 service_set_state(s, SERVICE_DEAD);
3004 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3005 [SERVICE_DEAD] = "dead",
3006 [SERVICE_START_PRE] = "start-pre",
3007 [SERVICE_START] = "start",
3008 [SERVICE_START_POST] = "start-post",
3009 [SERVICE_RUNNING] = "running",
3010 [SERVICE_EXITED] = "exited",
3011 [SERVICE_RELOAD] = "reload",
3012 [SERVICE_STOP] = "stop",
3013 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3014 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3015 [SERVICE_STOP_POST] = "stop-post",
3016 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3017 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3018 [SERVICE_FAILED] = "failed",
3019 [SERVICE_AUTO_RESTART] = "auto-restart",
3022 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3024 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3025 [SERVICE_ONCE] = "once",
3026 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
3027 [SERVICE_RESTART_ALWAYS] = "restart-always",
3030 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3032 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3033 [SERVICE_SIMPLE] = "simple",
3034 [SERVICE_FORKING] = "forking",
3035 [SERVICE_ONESHOT] = "oneshot",
3036 [SERVICE_DBUS] = "dbus",
3037 [SERVICE_NOTIFY] = "notify"
3040 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3042 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3043 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3044 [SERVICE_EXEC_START] = "ExecStart",
3045 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3046 [SERVICE_EXEC_RELOAD] = "ExecReload",
3047 [SERVICE_EXEC_STOP] = "ExecStop",
3048 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3051 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3053 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3054 [NOTIFY_NONE] = "none",
3055 [NOTIFY_MAIN] = "main",
3056 [NOTIFY_ALL] = "all"
3059 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3061 const UnitVTable service_vtable = {
3062 .suffix = ".service",
3063 .show_status = true,
3065 .init = service_init,
3066 .done = service_done,
3067 .load = service_load,
3069 .coldplug = service_coldplug,
3071 .dump = service_dump,
3073 .start = service_start,
3074 .stop = service_stop,
3075 .reload = service_reload,
3077 .can_reload = service_can_reload,
3079 .serialize = service_serialize,
3080 .deserialize_item = service_deserialize_item,
3082 .active_state = service_active_state,
3083 .sub_state_to_string = service_sub_state_to_string,
3085 #ifdef HAVE_SYSV_COMPAT
3086 .check_gc = service_check_gc,
3088 .check_snapshot = service_check_snapshot,
3090 .sigchld_event = service_sigchld_event,
3091 .timer_event = service_timer_event,
3093 .reset_failed = service_reset_failed,
3095 .cgroup_notify_empty = service_cgroup_notify_event,
3096 .notify_message = service_notify_message,
3098 .bus_name_owner_change = service_bus_name_owner_change,
3099 .bus_query_pid_done = service_bus_query_pid_done,
3101 .bus_interface = "org.freedesktop.systemd1.Service",
3102 .bus_message_handler = bus_service_message_handler,
3103 .bus_invalidating_properties = bus_service_invalidating_properties,
3105 #ifdef HAVE_SYSV_COMPAT
3106 .enumerate = service_enumerate