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 typedef enum RunlevelType {
50 const RunlevelType type;
52 /* Standard SysV runlevels for start-up */
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 },
60 /* SUSE style boot.d */
61 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
65 /* Debian style rcS.d */
66 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
69 /* Standard SysV runlevels for shutdown */
70 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
71 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
73 /* Note that the order here matters, as we read the
74 directories in this order, and we want to make sure that
75 sysv_start_priority is known when we first load the
76 unit. And that value we only know from S links. Hence
77 UP/SYSINIT must be read before DOWN */
80 #define RUNLEVELS_UP "12345"
81 /* #define RUNLEVELS_DOWN "06" */
82 /* #define RUNLEVELS_BOOT "bBsS" */
84 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
85 [SERVICE_DEAD] = UNIT_INACTIVE,
86 [SERVICE_START_PRE] = UNIT_ACTIVATING,
87 [SERVICE_START] = UNIT_ACTIVATING,
88 [SERVICE_START_POST] = UNIT_ACTIVATING,
89 [SERVICE_RUNNING] = UNIT_ACTIVE,
90 [SERVICE_EXITED] = UNIT_ACTIVE,
91 [SERVICE_RELOAD] = UNIT_RELOADING,
92 [SERVICE_STOP] = UNIT_DEACTIVATING,
93 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
94 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
95 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
96 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
97 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
98 [SERVICE_FAILED] = UNIT_FAILED,
99 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
102 static void service_init(Unit *u) {
103 Service *s = SERVICE(u);
106 assert(u->meta.load_state == UNIT_STUB);
108 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
109 s->restart_usec = DEFAULT_RESTART_USEC;
110 s->timer_watch.type = WATCH_INVALID;
111 s->sysv_start_priority = -1;
114 exec_context_init(&s->exec_context);
116 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
118 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
121 static void service_unwatch_control_pid(Service *s) {
124 if (s->control_pid <= 0)
127 unit_unwatch_pid(UNIT(s), s->control_pid);
131 static void service_unwatch_main_pid(Service *s) {
134 if (s->main_pid <= 0)
137 unit_unwatch_pid(UNIT(s), s->main_pid);
141 static int service_set_main_pid(Service *s, pid_t pid) {
152 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
153 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
154 s->meta.id, (unsigned long) pid);
157 s->main_pid_known = true;
159 exec_status_start(&s->main_exec_status, pid);
164 static void service_close_socket_fd(Service *s) {
167 if (s->socket_fd < 0)
170 close_nointr_nofail(s->socket_fd);
174 static void service_connection_unref(Service *s) {
180 socket_connection_unref(s->socket);
184 static void service_done(Unit *u) {
185 Service *s = SERVICE(u);
195 free(s->sysv_runlevels);
196 s->sysv_runlevels = NULL;
198 free(s->status_text);
199 s->status_text = NULL;
201 exec_context_done(&s->exec_context);
202 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
203 s->control_command = NULL;
205 /* This will leak a process, but at least no memory or any of
207 service_unwatch_main_pid(s);
208 service_unwatch_control_pid(s);
211 unit_unwatch_bus_name(UNIT(u), s->bus_name);
216 service_close_socket_fd(s);
217 service_connection_unref(s);
219 unit_unwatch_timer(u, &s->timer_watch);
222 static char *sysv_translate_name(const char *name) {
225 if (!(r = new(char, strlen(name) + sizeof(".service"))))
228 if (startswith(name, "boot."))
229 /* Drop SuSE-style boot. prefix */
230 strcpy(stpcpy(r, name + 5), ".service");
231 else if (endswith(name, ".sh"))
232 /* Drop Debian-style .sh suffix */
233 strcpy(stpcpy(r, name) - 3, ".service");
235 /* Normal init scripts */
236 strcpy(stpcpy(r, name), ".service");
241 static int sysv_translate_facility(const char *name, char **_r) {
243 static const char * const table[] = {
244 /* LSB defined facilities */
245 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
246 "$network", SPECIAL_NETWORK_TARGET,
247 "$named", SPECIAL_NSS_LOOKUP_TARGET,
248 "$portmap", SPECIAL_RPCBIND_TARGET,
249 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
250 "$syslog", SPECIAL_SYSLOG_TARGET,
251 "$time", SPECIAL_RTC_SET_TARGET,
253 /* Debian extensions */
255 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
257 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
258 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
261 /* Fedora extensions, lacking the $ prefix */
262 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
263 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
270 for (i = 0; i < ELEMENTSOF(table); i += 2)
271 if (streq(table[i], name)) {
272 if (!(r = strdup(table[i+1])))
281 if (!(r = sysv_translate_name(name)))
292 static int sysv_fix_order(Service *s) {
298 if (s->sysv_start_priority < 0)
301 /* For each pair of services where at least one lacks a LSB
302 * header, we use the start priority value to order things. */
304 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
307 bool special_s, special_t;
309 t = (Service*) other;
314 if (t->sysv_start_priority < 0)
317 /* If both units have modern headers we don't care
318 * about the priorities */
319 if ((!s->sysv_path || s->sysv_has_lsb) &&
320 (!t->sysv_path || t->sysv_has_lsb))
323 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
324 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
326 if (special_t && !special_s)
328 else if (special_s && !special_t)
330 else if (t->sysv_start_priority < s->sysv_start_priority)
332 else if (t->sysv_start_priority > s->sysv_start_priority)
337 /* FIXME: Maybe we should compare the name here lexicographically? */
339 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
346 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
349 if (!(c = new0(ExecCommand, 1)))
352 if (!(c->path = strdup(path))) {
357 if (!(c->argv = strv_new(path, arg1, NULL))) {
366 static int sysv_exec_commands(Service *s) {
370 assert(s->sysv_path);
372 if (!(c = exec_command_new(s->sysv_path, "start")))
374 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
376 if (!(c = exec_command_new(s->sysv_path, "stop")))
378 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
380 if (!(c = exec_command_new(s->sysv_path, "reload")))
382 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
387 static int service_load_sysv_path(Service *s, const char *path) {
404 if (!(f = fopen(path, "re"))) {
405 r = errno == ENOENT ? 0 : -errno;
410 if (!(s->sysv_path = strdup(path))) {
416 char l[LINE_MAX], *t;
418 if (!fgets(l, sizeof(l), f)) {
423 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
433 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
435 s->sysv_has_lsb = true;
439 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
445 t += strspn(t, WHITESPACE);
447 if (state == NORMAL) {
449 /* Try to parse Red Hat style chkconfig headers */
451 if (startswith_no_case(t, "chkconfig:")) {
453 char runlevels[16], *k;
457 if (sscanf(t+10, "%15s %i %*i",
459 &start_priority) != 2) {
461 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
465 /* A start priority gathered from the
466 * symlink farms is preferred over the
467 * data from the LSB header. */
468 if (start_priority < 0 || start_priority > 99)
469 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
470 else if (s->sysv_start_priority < 0)
471 s->sysv_start_priority = start_priority;
473 char_array_0(runlevels);
474 k = delete_chars(runlevels, WHITESPACE "-");
479 if (!(d = strdup(k))) {
484 free(s->sysv_runlevels);
485 s->sysv_runlevels = d;
488 } else if (startswith_no_case(t, "description:") &&
489 !u->meta.description) {
491 size_t k = strlen(t);
494 if (t[k-1] == '\\') {
499 if (!(d = strappend("LSB: ", strstrip(t+12)))) {
504 free(u->meta.description);
505 u->meta.description = d;
507 } else if (startswith_no_case(t, "pidfile:")) {
514 if (!path_is_absolute(fn)) {
515 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
519 if (!(fn = strdup(fn))) {
528 } else if (state == DESCRIPTION) {
530 /* Try to parse Red Hat style description
533 size_t k = strlen(t);
541 assert(u->meta.description);
542 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
547 free(u->meta.description);
548 u->meta.description = d;
550 } else if (state == LSB || state == LSB_DESCRIPTION) {
552 if (startswith_no_case(t, "Provides:")) {
558 FOREACH_WORD_QUOTED(w, z, t+9, i) {
561 if (!(n = strndup(w, z))) {
566 r = sysv_translate_facility(n, &m);
575 if (unit_name_to_type(m) == UNIT_SERVICE)
576 r = unit_add_name(u, m);
578 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
580 if (s->sysv_enabled) {
583 if ((k = unit_add_dependency_by_name_inverse(u, UNIT_WANTS, m, NULL, true)) < 0)
589 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
594 } else if (startswith_no_case(t, "Required-Start:") ||
595 startswith_no_case(t, "Should-Start:") ||
596 startswith_no_case(t, "X-Start-Before:") ||
597 startswith_no_case(t, "X-Start-After:")) {
603 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
606 if (!(n = strndup(w, z))) {
611 r = sysv_translate_facility(n, &m);
620 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
623 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
627 } else if (startswith_no_case(t, "Default-Start:")) {
632 k = delete_chars(t+14, WHITESPACE "-");
635 if (!(d = strdup(k))) {
640 free(s->sysv_runlevels);
641 s->sysv_runlevels = d;
644 } else if (startswith_no_case(t, "Description:") &&
645 !u->meta.description) {
648 /* We use the long description only if
649 * no short description is set. */
651 state = LSB_DESCRIPTION;
653 if (!(d = strappend("LSB: ", strstrip(t+12)))) {
658 free(u->meta.description);
659 u->meta.description = d;
661 } else if (startswith_no_case(t, "Short-Description:")) {
666 if (!(d = strappend("LSB: ", strstrip(t+18)))) {
671 free(u->meta.description);
672 u->meta.description = d;
674 } else if (startswith_no_case(t, "X-Interactive:")) {
677 if ((b = parse_boolean(strstrip(t+14))) < 0) {
678 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
683 s->exec_context.std_input = EXEC_INPUT_TTY;
685 s->exec_context.std_input = EXEC_INPUT_NULL;
687 } else if (state == LSB_DESCRIPTION) {
689 if (startswith(l, "#\t") || startswith(l, "# ")) {
692 assert(u->meta.description);
693 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
698 free(u->meta.description);
699 u->meta.description = d;
706 if ((r = sysv_exec_commands(s)) < 0)
709 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
710 /* If there a runlevels configured for this service
711 * but none of the standard ones, then we assume this
712 * is some special kind of service (which might be
713 * needed for early boot) and don't create any links
716 s->meta.default_dependencies = false;
718 /* Don't timeout special services during boot (like fsck) */
722 /* Special setting for all SysV services */
723 s->type = SERVICE_FORKING;
724 s->remain_after_exit = true;
725 s->restart = SERVICE_ONCE;
726 s->exec_context.std_output =
727 (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY)
728 ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
729 s->exec_context.kill_mode = KILL_PROCESS_GROUP;
731 u->meta.load_state = UNIT_LOADED;
742 static int service_load_sysv_name(Service *s, const char *name) {
748 /* For SysV services we strip the boot. or .sh
749 * prefixes/suffixes. */
750 if (startswith(name, "boot.") ||
751 endswith(name, ".sh.service"))
754 STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
758 if (asprintf(&path, "%s/%s", *p, name) < 0)
761 assert(endswith(path, ".service"));
762 path[strlen(path)-8] = 0;
764 r = service_load_sysv_path(s, path);
766 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
767 /* Try Debian style xxx.sh source'able init scripts */
769 r = service_load_sysv_path(s, path);
774 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
775 /* Try SUSE style boot.xxx init scripts */
777 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
780 path[strlen(path)-8] = 0;
781 r = service_load_sysv_path(s, path);
788 if ((s->meta.load_state != UNIT_STUB))
795 static int service_load_sysv(Service *s) {
802 /* Load service data from SysV init scripts, preferably with
805 if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
808 if ((t = s->meta.id))
809 if ((r = service_load_sysv_name(s, t)) < 0)
812 if (s->meta.load_state == UNIT_STUB)
813 SET_FOREACH(t, s->meta.names, i) {
817 if ((r = service_load_sysv_name(s, t)) < 0)
820 if (s->meta.load_state != UNIT_STUB)
827 static int service_add_bus_name(Service *s) {
834 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
837 r = unit_merge_by_name(UNIT(s), n);
843 static int service_verify(Service *s) {
846 if (s->meta.load_state != UNIT_LOADED)
849 if (!s->exec_command[SERVICE_EXEC_START]) {
850 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
854 if (s->type != SERVICE_ONESHOT &&
855 s->exec_command[SERVICE_EXEC_START]->command_next) {
856 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
860 if (s->type == SERVICE_DBUS && !s->bus_name) {
861 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
865 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
866 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
873 static int service_add_default_dependencies(Service *s) {
878 /* Add a number of automatic dependencies useful for the
879 * majority of services. */
881 /* First, pull in base system */
882 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
884 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
887 } else if (s->meta.manager->running_as == MANAGER_SESSION) {
889 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
893 /* Second, activate normal shutdown */
894 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
897 static int service_load(Unit *u) {
899 Service *s = SERVICE(u);
903 /* Load a .service file */
904 if ((r = unit_load_fragment(u)) < 0)
907 /* Load a classic init script as a fallback, if we couldn't find anything */
908 if (u->meta.load_state == UNIT_STUB)
909 if ((r = service_load_sysv(s)) < 0)
912 /* Still nothing found? Then let's give up */
913 if (u->meta.load_state == UNIT_STUB)
916 /* We were able to load something, then let's add in the
917 * dropin directories. */
918 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
921 /* This is a new unit? Then let's add in some extras */
922 if (u->meta.load_state == UNIT_LOADED) {
923 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
926 if ((r = unit_add_default_cgroup(u)) < 0)
929 if ((r = sysv_fix_order(s)) < 0)
933 if ((r = service_add_bus_name(s)) < 0)
936 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
940 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
941 s->notify_access = NOTIFY_MAIN;
943 if (s->type == SERVICE_DBUS || s->bus_name)
944 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
947 if (s->meta.default_dependencies)
948 if ((r = service_add_default_dependencies(s)) < 0)
952 return service_verify(s);
955 static void service_dump(Unit *u, FILE *f, const char *prefix) {
957 ServiceExecCommand c;
958 Service *s = SERVICE(u);
964 p2 = strappend(prefix, "\t");
965 prefix2 = p2 ? p2 : prefix;
968 "%sService State: %s\n"
969 "%sPermissionsStartOnly: %s\n"
970 "%sRootDirectoryStartOnly: %s\n"
971 "%sRemainAfterExit: %s\n"
974 "%sNotifyAccess: %s\n",
975 prefix, service_state_to_string(s->state),
976 prefix, yes_no(s->permissions_start_only),
977 prefix, yes_no(s->root_directory_start_only),
978 prefix, yes_no(s->remain_after_exit),
979 prefix, service_type_to_string(s->type),
980 prefix, service_restart_to_string(s->restart),
981 prefix, notify_access_to_string(s->notify_access));
983 if (s->control_pid > 0)
985 "%sControl PID: %lu\n",
986 prefix, (unsigned long) s->control_pid);
991 prefix, (unsigned long) s->main_pid);
996 prefix, s->pid_file);
1001 "%sBus Name Good: %s\n",
1002 prefix, s->bus_name,
1003 prefix, yes_no(s->bus_name_good));
1005 exec_context_dump(&s->exec_context, f, prefix);
1007 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1009 if (!s->exec_command[c])
1012 fprintf(f, "%s-> %s:\n",
1013 prefix, service_exec_command_to_string(c));
1015 exec_command_dump_list(s->exec_command[c], f, prefix2);
1020 "%sSysV Init Script Path: %s\n"
1021 "%sSysV Init Script has LSB Header: %s\n",
1022 prefix, s->sysv_path,
1023 prefix, yes_no(s->sysv_has_lsb));
1025 if (s->sysv_start_priority >= 0)
1027 "%sSysVStartPriority: %i\n"
1028 "%sSysVEnabled: %s\n",
1029 prefix, s->sysv_start_priority,
1030 prefix, yes_no(s->sysv_enabled));
1032 if (s->sysv_runlevels)
1033 fprintf(f, "%sSysVRunLevels: %s\n",
1034 prefix, s->sysv_runlevels);
1037 fprintf(f, "%sStatus Text: %s\n",
1038 prefix, s->status_text);
1043 static int service_load_pid_file(Service *s) {
1050 if (s->main_pid_known)
1053 assert(s->main_pid <= 0);
1058 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1061 r = parse_pid(k, &pid);
1067 if (kill(pid, 0) < 0 && errno != EPERM) {
1068 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1069 (unsigned long) pid, s->pid_file);
1073 if ((r = service_set_main_pid(s, pid)) < 0)
1076 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1077 /* FIXME: we need to do something here */
1083 static int service_get_sockets(Service *s, Set **_set) {
1092 if (s->socket_fd >= 0)
1095 /* Collects all Socket objects that belong to this
1096 * service. Note that a service might have multiple sockets
1097 * via multiple names. */
1099 if (!(set = set_new(NULL, NULL)))
1102 SET_FOREACH(t, s->meta.names, i) {
1106 /* Look for all socket objects that go by any of our
1107 * units and collect their fds */
1109 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1114 p = manager_get_unit(s->meta.manager, k);
1120 if ((r = set_put(set, p)) < 0)
1132 static int service_notify_sockets_dead(Service *s) {
1140 if (s->socket_fd >= 0)
1143 /* Notifies all our sockets when we die */
1144 if ((r = service_get_sockets(s, &set)) < 0)
1147 SET_FOREACH(sock, set, i)
1148 socket_notify_service_dead(sock);
1155 static void service_set_state(Service *s, ServiceState state) {
1156 ServiceState old_state;
1159 old_state = s->state;
1162 if (state != SERVICE_START_PRE &&
1163 state != SERVICE_START &&
1164 state != SERVICE_START_POST &&
1165 state != SERVICE_RELOAD &&
1166 state != SERVICE_STOP &&
1167 state != SERVICE_STOP_SIGTERM &&
1168 state != SERVICE_STOP_SIGKILL &&
1169 state != SERVICE_STOP_POST &&
1170 state != SERVICE_FINAL_SIGTERM &&
1171 state != SERVICE_FINAL_SIGKILL &&
1172 state != SERVICE_AUTO_RESTART)
1173 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1175 if (state != SERVICE_START &&
1176 state != SERVICE_START_POST &&
1177 state != SERVICE_RUNNING &&
1178 state != SERVICE_RELOAD &&
1179 state != SERVICE_STOP &&
1180 state != SERVICE_STOP_SIGTERM &&
1181 state != SERVICE_STOP_SIGKILL)
1182 service_unwatch_main_pid(s);
1184 if (state != SERVICE_START_PRE &&
1185 state != SERVICE_START &&
1186 state != SERVICE_START_POST &&
1187 state != SERVICE_RELOAD &&
1188 state != SERVICE_STOP &&
1189 state != SERVICE_STOP_SIGTERM &&
1190 state != SERVICE_STOP_SIGKILL &&
1191 state != SERVICE_STOP_POST &&
1192 state != SERVICE_FINAL_SIGTERM &&
1193 state != SERVICE_FINAL_SIGKILL) {
1194 service_unwatch_control_pid(s);
1195 s->control_command = NULL;
1196 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1199 if (state == SERVICE_DEAD ||
1200 state == SERVICE_STOP ||
1201 state == SERVICE_STOP_SIGTERM ||
1202 state == SERVICE_STOP_SIGKILL ||
1203 state == SERVICE_STOP_POST ||
1204 state == SERVICE_FINAL_SIGTERM ||
1205 state == SERVICE_FINAL_SIGKILL ||
1206 state == SERVICE_FAILED ||
1207 state == SERVICE_AUTO_RESTART)
1208 service_notify_sockets_dead(s);
1210 if (state != SERVICE_START_PRE &&
1211 state != SERVICE_START &&
1212 state != SERVICE_START_POST &&
1213 state != SERVICE_RUNNING &&
1214 state != SERVICE_RELOAD &&
1215 state != SERVICE_STOP &&
1216 state != SERVICE_STOP_SIGTERM &&
1217 state != SERVICE_STOP_SIGKILL &&
1218 state != SERVICE_STOP_POST &&
1219 state != SERVICE_FINAL_SIGTERM &&
1220 state != SERVICE_FINAL_SIGKILL &&
1221 !(state == SERVICE_DEAD && s->meta.job)) {
1222 service_close_socket_fd(s);
1223 service_connection_unref(s);
1226 /* For the inactive states unit_notify() will trim the cgroup,
1227 * but for exit we have to do that ourselves... */
1228 if (state == SERVICE_EXITED)
1229 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1231 if (old_state != state)
1232 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1234 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1237 static int service_coldplug(Unit *u) {
1238 Service *s = SERVICE(u);
1242 assert(s->state == SERVICE_DEAD);
1244 if (s->deserialized_state != s->state) {
1246 if (s->deserialized_state == SERVICE_START_PRE ||
1247 s->deserialized_state == SERVICE_START ||
1248 s->deserialized_state == SERVICE_START_POST ||
1249 s->deserialized_state == SERVICE_RELOAD ||
1250 s->deserialized_state == SERVICE_STOP ||
1251 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1252 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1253 s->deserialized_state == SERVICE_STOP_POST ||
1254 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1255 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1256 s->deserialized_state == SERVICE_AUTO_RESTART) {
1258 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1261 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1263 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1268 if ((s->deserialized_state == SERVICE_START &&
1269 (s->type == SERVICE_FORKING ||
1270 s->type == SERVICE_DBUS ||
1271 s->type == SERVICE_ONESHOT ||
1272 s->type == SERVICE_NOTIFY)) ||
1273 s->deserialized_state == SERVICE_START_POST ||
1274 s->deserialized_state == SERVICE_RUNNING ||
1275 s->deserialized_state == SERVICE_RELOAD ||
1276 s->deserialized_state == SERVICE_STOP ||
1277 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1278 s->deserialized_state == SERVICE_STOP_SIGKILL)
1279 if (s->main_pid > 0)
1280 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1283 if (s->deserialized_state == SERVICE_START_PRE ||
1284 s->deserialized_state == SERVICE_START ||
1285 s->deserialized_state == SERVICE_START_POST ||
1286 s->deserialized_state == SERVICE_RELOAD ||
1287 s->deserialized_state == SERVICE_STOP ||
1288 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1289 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1290 s->deserialized_state == SERVICE_STOP_POST ||
1291 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1292 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1293 if (s->control_pid > 0)
1294 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1297 service_set_state(s, s->deserialized_state);
1303 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1307 unsigned rn_fds = 0;
1315 if (s->socket_fd >= 0)
1318 if ((r = service_get_sockets(s, &set)) < 0)
1321 SET_FOREACH(sock, set, i) {
1325 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1337 if (!(t = new(int, rn_fds+cn_fds))) {
1343 memcpy(t, rfds, rn_fds);
1344 memcpy(t+rn_fds, cfds, cn_fds);
1349 rn_fds = rn_fds+cn_fds;
1367 static int service_spawn(
1372 bool apply_permissions,
1374 bool apply_tty_stdin,
1375 bool set_notify_socket,
1380 int *fds = NULL, *fdsbuf = NULL;
1381 unsigned n_fds = 0, n_env = 0;
1382 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1389 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1390 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1391 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1393 if (s->socket_fd >= 0) {
1394 fds = &s->socket_fd;
1397 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1404 if (timeout && s->timeout_usec) {
1405 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1408 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1410 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1415 if (!(our_env = new0(char*, 3))) {
1420 if (set_notify_socket)
1421 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1426 if (s->main_pid > 0)
1427 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1432 if (!(final_env = strv_env_merge(2,
1433 s->meta.manager->environment,
1448 s->meta.manager->confirm_spawn,
1449 s->meta.cgroup_bondings,
1456 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1457 /* FIXME: we need to do something here */
1463 strv_free(final_env);
1473 strv_free(final_env);
1476 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1481 static int main_pid_good(Service *s) {
1484 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1487 /* If we know the pid file, then lets just check if it is
1489 if (s->main_pid_known)
1490 return s->main_pid > 0;
1492 /* We don't know the pid */
1496 static int control_pid_good(Service *s) {
1499 return s->control_pid > 0;
1502 static int cgroup_good(Service *s) {
1507 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1513 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1520 if (allow_restart &&
1521 !s->forbid_restart &&
1522 (s->restart == SERVICE_RESTART_ALWAYS ||
1523 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1525 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1528 service_set_state(s, SERVICE_AUTO_RESTART);
1530 service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD);
1532 s->forbid_restart = false;
1537 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1538 service_enter_dead(s, false, false);
1541 static void service_enter_signal(Service *s, ServiceState state, bool success);
1543 static void service_enter_stop_post(Service *s, bool success) {
1550 service_unwatch_control_pid(s);
1552 s->control_command_id = SERVICE_EXEC_STOP_POST;
1553 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1554 if ((r = service_spawn(s,
1558 !s->permissions_start_only,
1559 !s->root_directory_start_only,
1562 &s->control_pid)) < 0)
1566 service_set_state(s, SERVICE_STOP_POST);
1568 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1573 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1574 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1577 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1579 Set *pid_set = NULL;
1580 bool wait_for_exit = false;
1587 if (s->exec_context.kill_mode != KILL_NONE) {
1588 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1590 if (s->main_pid > 0) {
1591 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1593 s->main_pid, sig) < 0 && errno != ESRCH)
1595 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1597 wait_for_exit = true;
1600 if (s->control_pid > 0) {
1601 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1603 s->control_pid, sig) < 0 && errno != ESRCH)
1605 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1607 wait_for_exit = true;
1610 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1612 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1617 /* Exclude the main/control pids from being killed via the cgroup */
1618 if (s->main_pid > 0)
1619 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1622 if (s->control_pid > 0)
1623 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1626 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
1627 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1628 log_warning("Failed to kill control group: %s", strerror(-r));
1630 wait_for_exit = true;
1636 if (wait_for_exit) {
1637 if (s->timeout_usec > 0)
1638 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1641 service_set_state(s, state);
1642 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1643 service_enter_stop_post(s, true);
1645 service_enter_dead(s, true, true);
1650 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1652 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1653 service_enter_stop_post(s, false);
1655 service_enter_dead(s, false, true);
1661 static void service_enter_stop(Service *s, bool success) {
1669 service_unwatch_control_pid(s);
1671 s->control_command_id = SERVICE_EXEC_STOP;
1672 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1673 if ((r = service_spawn(s,
1677 !s->permissions_start_only,
1678 !s->root_directory_start_only,
1681 &s->control_pid)) < 0)
1684 service_set_state(s, SERVICE_STOP);
1686 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1691 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1692 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1695 static void service_enter_running(Service *s, bool success) {
1696 int main_pid_ok, cgroup_ok;
1702 main_pid_ok = main_pid_good(s);
1703 cgroup_ok = cgroup_good(s);
1705 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1706 (s->bus_name_good || s->type != SERVICE_DBUS))
1707 service_set_state(s, SERVICE_RUNNING);
1708 else if (s->remain_after_exit)
1709 service_set_state(s, SERVICE_EXITED);
1711 service_enter_stop(s, true);
1714 static void service_enter_start_post(Service *s) {
1718 service_unwatch_control_pid(s);
1720 s->control_command_id = SERVICE_EXEC_START_POST;
1721 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1722 if ((r = service_spawn(s,
1726 !s->permissions_start_only,
1727 !s->root_directory_start_only,
1730 &s->control_pid)) < 0)
1733 service_set_state(s, SERVICE_START_POST);
1735 service_enter_running(s, true);
1740 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1741 service_enter_stop(s, false);
1744 static void service_enter_start(Service *s) {
1750 assert(s->exec_command[SERVICE_EXEC_START]);
1751 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1753 if (s->type == SERVICE_FORKING)
1754 service_unwatch_control_pid(s);
1756 service_unwatch_main_pid(s);
1758 s->control_command_id = SERVICE_EXEC_START;
1759 s->control_command = s->exec_command[SERVICE_EXEC_START];
1761 if ((r = service_spawn(s,
1763 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1768 s->notify_access != NOTIFY_NONE,
1772 if (s->type == SERVICE_SIMPLE) {
1773 /* For simple services we immediately start
1774 * the START_POST binaries. */
1776 service_set_main_pid(s, pid);
1777 service_enter_start_post(s);
1779 } else if (s->type == SERVICE_FORKING) {
1781 /* For forking services we wait until the start
1782 * process exited. */
1784 s->control_pid = pid;
1785 service_set_state(s, SERVICE_START);
1787 } else if (s->type == SERVICE_ONESHOT ||
1788 s->type == SERVICE_DBUS ||
1789 s->type == SERVICE_NOTIFY) {
1791 /* For oneshot services we wait until the start
1792 * process exited, too, but it is our main process. */
1794 /* For D-Bus services we know the main pid right away,
1795 * but wait for the bus name to appear on the
1796 * bus. Notify services are similar. */
1798 service_set_main_pid(s, pid);
1799 service_set_state(s, SERVICE_START);
1801 assert_not_reached("Unknown service type");
1806 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1807 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1810 static void service_enter_start_pre(Service *s) {
1815 service_unwatch_control_pid(s);
1817 s->control_command_id = SERVICE_EXEC_START_PRE;
1818 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1819 if ((r = service_spawn(s,
1823 !s->permissions_start_only,
1824 !s->root_directory_start_only,
1827 &s->control_pid)) < 0)
1830 service_set_state(s, SERVICE_START_PRE);
1832 service_enter_start(s);
1837 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1838 service_enter_dead(s, false, true);
1841 static void service_enter_restart(Service *s) {
1846 dbus_error_init(&error);
1848 service_enter_dead(s, true, false);
1850 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
1853 log_debug("%s scheduled restart job.", s->meta.id);
1857 log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
1858 service_enter_dead(s, false, false);
1860 dbus_error_free(&error);
1863 static void service_enter_reload(Service *s) {
1868 service_unwatch_control_pid(s);
1870 s->control_command_id = SERVICE_EXEC_RELOAD;
1871 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1872 if ((r = service_spawn(s,
1876 !s->permissions_start_only,
1877 !s->root_directory_start_only,
1880 &s->control_pid)) < 0)
1883 service_set_state(s, SERVICE_RELOAD);
1885 service_enter_running(s, true);
1890 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1891 service_enter_stop(s, false);
1894 static void service_run_next_control(Service *s, bool success) {
1898 assert(s->control_command);
1899 assert(s->control_command->command_next);
1904 assert(s->control_command_id != SERVICE_EXEC_START);
1906 s->control_command = s->control_command->command_next;
1907 service_unwatch_control_pid(s);
1909 if ((r = service_spawn(s,
1913 !s->permissions_start_only,
1914 !s->root_directory_start_only,
1915 s->control_command_id == SERVICE_EXEC_START_PRE ||
1916 s->control_command_id == SERVICE_EXEC_STOP_POST,
1918 &s->control_pid)) < 0)
1924 log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
1926 if (s->state == SERVICE_START_PRE)
1927 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1928 else if (s->state == SERVICE_STOP)
1929 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1930 else if (s->state == SERVICE_STOP_POST)
1931 service_enter_dead(s, false, true);
1933 service_enter_stop(s, false);
1936 static void service_run_next_main(Service *s, bool success) {
1941 assert(s->control_command);
1942 assert(s->control_command->command_next);
1947 assert(s->control_command_id == SERVICE_EXEC_START);
1948 assert(s->type == SERVICE_ONESHOT);
1950 s->control_command = s->control_command->command_next;
1951 service_unwatch_main_pid(s);
1953 if ((r = service_spawn(s,
1960 s->notify_access != NOTIFY_NONE,
1964 service_set_main_pid(s, pid);
1969 log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
1970 service_enter_stop(s, false);
1973 static int service_start(Unit *u) {
1974 Service *s = SERVICE(u);
1978 /* We cannot fulfill this request right now, try again later
1980 if (s->state == SERVICE_STOP ||
1981 s->state == SERVICE_STOP_SIGTERM ||
1982 s->state == SERVICE_STOP_SIGKILL ||
1983 s->state == SERVICE_STOP_POST ||
1984 s->state == SERVICE_FINAL_SIGTERM ||
1985 s->state == SERVICE_FINAL_SIGKILL)
1988 /* Already on it! */
1989 if (s->state == SERVICE_START_PRE ||
1990 s->state == SERVICE_START ||
1991 s->state == SERVICE_START_POST)
1994 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
1996 /* Make sure we don't enter a busy loop of some kind. */
1997 if (!ratelimit_test(&s->ratelimit)) {
1998 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
2002 if ((s->exec_context.std_input == EXEC_INPUT_SOCKET ||
2003 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
2004 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) &&
2006 log_warning("%s can only be started with a per-connection socket.", u->meta.id);
2011 s->main_pid_known = false;
2012 s->forbid_restart = false;
2014 service_enter_start_pre(s);
2018 static int service_stop(Unit *u) {
2019 Service *s = SERVICE(u);
2023 /* This is a user request, so don't do restarts on this
2025 s->forbid_restart = true;
2028 if (s->state == SERVICE_STOP ||
2029 s->state == SERVICE_STOP_SIGTERM ||
2030 s->state == SERVICE_STOP_SIGKILL ||
2031 s->state == SERVICE_STOP_POST ||
2032 s->state == SERVICE_FINAL_SIGTERM ||
2033 s->state == SERVICE_FINAL_SIGKILL)
2036 /* Don't allow a restart */
2037 if (s->state == SERVICE_AUTO_RESTART) {
2038 service_set_state(s, SERVICE_DEAD);
2042 /* If there's already something running we go directly into
2044 if (s->state == SERVICE_START_PRE ||
2045 s->state == SERVICE_START ||
2046 s->state == SERVICE_START_POST ||
2047 s->state == SERVICE_RELOAD) {
2048 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2052 assert(s->state == SERVICE_RUNNING ||
2053 s->state == SERVICE_EXITED);
2055 service_enter_stop(s, true);
2059 static int service_reload(Unit *u) {
2060 Service *s = SERVICE(u);
2064 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2066 service_enter_reload(s);
2070 static bool service_can_reload(Unit *u) {
2071 Service *s = SERVICE(u);
2075 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2078 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2079 Service *s = SERVICE(u);
2085 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2086 unit_serialize_item(u, f, "failure", yes_no(s->failure));
2088 if (s->control_pid > 0)
2089 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2091 if (s->main_pid_known && s->main_pid > 0)
2092 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2094 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2097 unit_serialize_item(u, f, "status-text", s->status_text);
2099 /* There's a minor uncleanliness here: if there are multiple
2100 * commands attached here, we will start from the first one
2102 if (s->control_command_id >= 0)
2103 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2105 if (s->socket_fd >= 0) {
2108 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2111 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2114 if (s->main_exec_status.pid > 0) {
2115 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2117 if (s->main_exec_status.start_timestamp.realtime > 0) {
2118 unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2119 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2121 unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2122 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2125 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2126 unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2127 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2128 unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2129 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2131 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2132 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2139 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2140 Service *s = SERVICE(u);
2147 if (streq(key, "state")) {
2150 if ((state = service_state_from_string(value)) < 0)
2151 log_debug("Failed to parse state value %s", value);
2153 s->deserialized_state = state;
2154 } else if (streq(key, "failure")) {
2157 if ((b = parse_boolean(value)) < 0)
2158 log_debug("Failed to parse failure value %s", value);
2160 s->failure = b || s->failure;
2161 } else if (streq(key, "control-pid")) {
2164 if (parse_pid(value, &pid) < 0)
2165 log_debug("Failed to parse control-pid value %s", value);
2167 s->control_pid = pid;
2168 } else if (streq(key, "main-pid")) {
2171 if (parse_pid(value, &pid) < 0)
2172 log_debug("Failed to parse main-pid value %s", value);
2174 service_set_main_pid(s, (pid_t) pid);
2175 } else if (streq(key, "main-pid-known")) {
2178 if ((b = parse_boolean(value)) < 0)
2179 log_debug("Failed to parse main-pid-known value %s", value);
2181 s->main_pid_known = b;
2182 } else if (streq(key, "status-text")) {
2185 if ((t = strdup(value))) {
2186 free(s->status_text);
2190 } else if (streq(key, "control-command")) {
2191 ServiceExecCommand id;
2193 if ((id = service_exec_command_from_string(value)) < 0)
2194 log_debug("Failed to parse exec-command value %s", value);
2196 s->control_command_id = id;
2197 s->control_command = s->exec_command[id];
2199 } else if (streq(key, "socket-fd")) {
2202 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2203 log_debug("Failed to parse socket-fd value %s", value);
2206 if (s->socket_fd >= 0)
2207 close_nointr_nofail(s->socket_fd);
2208 s->socket_fd = fdset_remove(fds, fd);
2210 } else if (streq(key, "main-exec-status-pid")) {
2213 if (parse_pid(value, &pid) < 0)
2214 log_debug("Failed to parse main-exec-status-pid value %s", value);
2216 s->main_exec_status.pid = pid;
2217 } else if (streq(key, "main-exec-status-code")) {
2220 if (safe_atoi(value, &i) < 0)
2221 log_debug("Failed to parse main-exec-status-code value %s", value);
2223 s->main_exec_status.code = i;
2224 } else if (streq(key, "main-exec-status-status")) {
2227 if (safe_atoi(value, &i) < 0)
2228 log_debug("Failed to parse main-exec-status-status value %s", value);
2230 s->main_exec_status.status = i;
2231 } else if (streq(key, "main-exec-status-start-realtime")) {
2234 if (safe_atou64(value, &k) < 0)
2235 log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2237 s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2238 } else if (streq(key, "main-exec-status-start-monotonic")) {
2241 if (safe_atou64(value, &k) < 0)
2242 log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2244 s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2245 } else if (streq(key, "main-exec-status-exit-realtime")) {
2248 if (safe_atou64(value, &k) < 0)
2249 log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2251 s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2252 } else if (streq(key, "main-exec-status-exit-monotonic")) {
2255 if (safe_atou64(value, &k) < 0)
2256 log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2258 s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2260 log_debug("Unknown serialization key '%s'", key);
2265 static UnitActiveState service_active_state(Unit *u) {
2268 return state_translation_table[SERVICE(u)->state];
2271 static const char *service_sub_state_to_string(Unit *u) {
2274 return service_state_to_string(SERVICE(u)->state);
2277 static bool service_check_gc(Unit *u) {
2278 Service *s = SERVICE(u);
2282 return !!s->sysv_path;
2285 static bool service_check_snapshot(Unit *u) {
2286 Service *s = SERVICE(u);
2290 return !s->got_socket_fd;
2293 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2294 Service *s = SERVICE(u);
2301 success = is_clean_exit_lsb(code, status);
2303 success = is_clean_exit(code, status);
2305 if (s->main_pid == pid) {
2308 exec_status_exit(&s->main_exec_status, pid, code, status);
2310 if (s->type != SERVICE_FORKING && s->control_command) {
2311 s->control_command->exec_status = s->main_exec_status;
2313 if (s->control_command->ignore)
2317 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2318 "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2319 s->failure = s->failure || !success;
2321 if (s->control_command &&
2322 s->control_command->command_next &&
2325 /* There is another command to *
2326 * execute, so let's do that. */
2328 log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2329 service_run_next_main(s, success);
2333 /* The service exited, so the service is officially
2336 s->control_command = NULL;
2337 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2341 case SERVICE_START_POST:
2342 case SERVICE_RELOAD:
2344 /* Need to wait until the operation is
2349 if (s->type == SERVICE_ONESHOT) {
2350 /* This was our main goal, so let's go on */
2352 service_enter_start_post(s);
2354 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2357 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2362 case SERVICE_RUNNING:
2363 service_enter_running(s, success);
2366 case SERVICE_STOP_SIGTERM:
2367 case SERVICE_STOP_SIGKILL:
2369 if (!control_pid_good(s))
2370 service_enter_stop_post(s, success);
2372 /* If there is still a control process, wait for that first */
2376 assert_not_reached("Uh, main process died at wrong time.");
2380 } else if (s->control_pid == pid) {
2384 if (s->control_command) {
2385 exec_status_exit(&s->control_command->exec_status, pid, code, status);
2387 if (s->control_command->ignore)
2391 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2392 "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2393 s->failure = s->failure || !success;
2395 if (s->control_command &&
2396 s->control_command->command_next &&
2399 /* There is another command to *
2400 * execute, so let's do that. */
2402 log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2403 service_run_next_control(s, success);
2406 /* No further commands for this step, so let's
2407 * figure out what to do next */
2409 s->control_command = NULL;
2410 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2412 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2416 case SERVICE_START_PRE:
2418 service_enter_start(s);
2420 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2424 assert(s->type == SERVICE_FORKING);
2426 /* Let's try to load the pid
2427 * file here if we can. We
2428 * ignore the return value,
2429 * since the PID file might
2430 * actually be created by a
2431 * START_POST script */
2435 service_load_pid_file(s);
2437 service_enter_start_post(s);
2439 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2443 case SERVICE_START_POST:
2444 if (success && s->pid_file && !s->main_pid_known) {
2447 /* Hmm, let's see if we can
2448 * load the pid now after the
2449 * start-post scripts got
2452 if ((r = service_load_pid_file(s)) < 0)
2453 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2458 case SERVICE_RELOAD:
2460 service_enter_running(s, true);
2462 service_enter_stop(s, false);
2467 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2470 case SERVICE_STOP_SIGTERM:
2471 case SERVICE_STOP_SIGKILL:
2472 if (main_pid_good(s) <= 0)
2473 service_enter_stop_post(s, success);
2475 /* If there is still a service
2476 * process around, wait until
2477 * that one quit, too */
2480 case SERVICE_STOP_POST:
2481 case SERVICE_FINAL_SIGTERM:
2482 case SERVICE_FINAL_SIGKILL:
2483 service_enter_dead(s, success, true);
2487 assert_not_reached("Uh, control process died at wrong time.");
2492 /* Notify clients about changed exit status */
2493 unit_add_to_dbus_queue(u);
2496 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2497 Service *s = SERVICE(u);
2500 assert(elapsed == 1);
2502 assert(w == &s->timer_watch);
2506 case SERVICE_START_PRE:
2508 log_warning("%s operation timed out. Terminating.", u->meta.id);
2509 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2512 case SERVICE_START_POST:
2513 case SERVICE_RELOAD:
2514 log_warning("%s operation timed out. Stopping.", u->meta.id);
2515 service_enter_stop(s, false);
2519 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2520 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2523 case SERVICE_STOP_SIGTERM:
2524 log_warning("%s stopping timed out. Killing.", u->meta.id);
2525 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2528 case SERVICE_STOP_SIGKILL:
2529 /* Uh, wie sent a SIGKILL and it is still not gone?
2530 * Must be something we cannot kill, so let's just be
2531 * weirded out and continue */
2533 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2534 service_enter_stop_post(s, false);
2537 case SERVICE_STOP_POST:
2538 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2539 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2542 case SERVICE_FINAL_SIGTERM:
2543 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2544 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2547 case SERVICE_FINAL_SIGKILL:
2548 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
2549 service_enter_dead(s, false, true);
2552 case SERVICE_AUTO_RESTART:
2553 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2554 service_enter_restart(s);
2558 assert_not_reached("Timeout at wrong time.");
2562 static void service_cgroup_notify_event(Unit *u) {
2563 Service *s = SERVICE(u);
2567 log_debug("%s: cgroup is empty", u->meta.id);
2571 /* Waiting for SIGCHLD is usually more interesting,
2572 * because it includes return codes/signals. Which is
2573 * why we ignore the cgroup events for most cases,
2574 * except when we don't know pid which to expect the
2577 case SERVICE_RUNNING:
2578 service_enter_running(s, true);
2581 case SERVICE_STOP_SIGTERM:
2582 case SERVICE_STOP_SIGKILL:
2583 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2584 service_enter_stop_post(s, true);
2588 case SERVICE_FINAL_SIGTERM:
2589 case SERVICE_FINAL_SIGKILL:
2590 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2591 service_enter_dead(s, true, true);
2600 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2601 Service *s = SERVICE(u);
2606 if (s->notify_access == NOTIFY_NONE) {
2607 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2608 u->meta.id, (unsigned long) pid);
2612 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2613 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2614 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2618 log_debug("%s: Got message", u->meta.id);
2620 /* Interpret MAINPID= */
2621 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2622 (s->state == SERVICE_START ||
2623 s->state == SERVICE_START_POST ||
2624 s->state == SERVICE_RUNNING ||
2625 s->state == SERVICE_RELOAD)) {
2627 if (parse_pid(e + 8, &pid) < 0)
2628 log_warning("Failed to parse notification message %s", e);
2630 log_debug("%s: got %s", u->meta.id, e);
2631 service_set_main_pid(s, pid);
2635 /* Interpret READY= */
2636 if (s->type == SERVICE_NOTIFY &&
2637 s->state == SERVICE_START &&
2638 strv_find(tags, "READY=1")) {
2639 log_debug("%s: got READY=1", u->meta.id);
2641 service_enter_start_post(s);
2644 /* Interpret STATUS= */
2645 if ((e = strv_find_prefix(tags, "STATUS="))) {
2649 if (!(t = strdup(e+7))) {
2650 log_error("Failed to allocate string.");
2654 log_debug("%s: got %s", u->meta.id, e);
2656 free(s->status_text);
2659 free(s->status_text);
2660 s->status_text = NULL;
2665 /* Notify clients about changed status or main pid */
2666 unit_add_to_dbus_queue(u);
2669 static int service_enumerate(Manager *m) {
2673 char *path = NULL, *fpath = NULL, *name = NULL;
2674 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
2681 zero(runlevel_services);
2683 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2684 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2689 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2697 if (!(d = opendir(path))) {
2698 if (errno != ENOENT)
2699 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2704 while ((de = readdir(d))) {
2707 if (ignore_file(de->d_name))
2710 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2713 if (strlen(de->d_name) < 4)
2716 a = undecchar(de->d_name[1]);
2717 b = undecchar(de->d_name[2]);
2724 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2729 if (access(fpath, X_OK) < 0) {
2731 if (errno != ENOENT)
2732 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2738 if (!(name = sysv_translate_name(de->d_name + 3))) {
2743 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2744 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2748 if (de->d_name[0] == 'S') {
2750 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
2751 SERVICE(service)->sysv_start_priority =
2752 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2754 SERVICE(service)->sysv_enabled = true;
2757 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
2760 if ((r = set_put(runlevel_services[i], service)) < 0)
2763 } else if (de->d_name[0] == 'K' &&
2764 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2765 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2767 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
2770 if ((r = set_put(shutdown_services, service)) < 0)
2776 /* Now we loaded all stubs and are aware of the lowest
2777 start-up priority for all services, not let's actually load
2778 the services, this will also tell us which services are
2779 actually native now */
2780 manager_dispatch_load_queue(m);
2782 /* If this is a native service, rely on native ways to pull in
2783 * a service, don't pull it in via sysv rcN.d links. */
2784 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
2785 SET_FOREACH(service, runlevel_services[i], j) {
2786 service = unit_follow_merge(service);
2788 if (service->meta.fragment_path)
2791 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2795 /* We honour K links only for halt/reboot. For the normal
2796 * runlevels we assume the stop jobs will be implicitly added
2797 * by the core logic. Also, we don't really distuingish here
2798 * between the runlevels 0 and 6 and just add them to the
2799 * special shutdown target. On SUSE the boot.d/ runlevel is
2800 * also used for shutdown, so we add links for that too to the
2801 * shutdown target.*/
2802 SET_FOREACH(service, shutdown_services, j) {
2803 service = unit_follow_merge(service);
2805 if (service->meta.fragment_path)
2808 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2819 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
2820 set_free(runlevel_services[i]);
2821 set_free(shutdown_services);
2829 static void service_bus_name_owner_change(
2832 const char *old_owner,
2833 const char *new_owner) {
2835 Service *s = SERVICE(u);
2840 assert(streq(s->bus_name, name));
2841 assert(old_owner || new_owner);
2843 if (old_owner && new_owner)
2844 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2846 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2848 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2850 s->bus_name_good = !!new_owner;
2852 if (s->type == SERVICE_DBUS) {
2854 /* service_enter_running() will figure out what to
2856 if (s->state == SERVICE_RUNNING)
2857 service_enter_running(s, true);
2858 else if (s->state == SERVICE_START && new_owner)
2859 service_enter_start_post(s);
2861 } else if (new_owner &&
2863 (s->state == SERVICE_START ||
2864 s->state == SERVICE_START_POST ||
2865 s->state == SERVICE_RUNNING ||
2866 s->state == SERVICE_RELOAD)) {
2868 /* Try to acquire PID from bus service */
2869 log_debug("Trying to acquire PID from D-Bus name...");
2871 bus_query_pid(u->meta.manager, name);
2875 static void service_bus_query_pid_done(
2880 Service *s = SERVICE(u);
2885 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2887 if (s->main_pid <= 0 &&
2888 (s->state == SERVICE_START ||
2889 s->state == SERVICE_START_POST ||
2890 s->state == SERVICE_RUNNING ||
2891 s->state == SERVICE_RELOAD))
2892 service_set_main_pid(s, pid);
2895 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2899 /* This is called by the socket code when instantiating a new
2900 * service for a stream socket and the socket needs to be
2903 if (s->meta.load_state != UNIT_LOADED)
2906 if (s->socket_fd >= 0)
2909 if (s->state != SERVICE_DEAD)
2913 s->got_socket_fd = true;
2919 static void service_reset_failed(Unit *u) {
2920 Service *s = SERVICE(u);
2924 if (s->state == SERVICE_FAILED)
2925 service_set_state(s, SERVICE_DEAD);
2930 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2931 [SERVICE_DEAD] = "dead",
2932 [SERVICE_START_PRE] = "start-pre",
2933 [SERVICE_START] = "start",
2934 [SERVICE_START_POST] = "start-post",
2935 [SERVICE_RUNNING] = "running",
2936 [SERVICE_EXITED] = "exited",
2937 [SERVICE_RELOAD] = "reload",
2938 [SERVICE_STOP] = "stop",
2939 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2940 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2941 [SERVICE_STOP_POST] = "stop-post",
2942 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2943 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2944 [SERVICE_FAILED] = "failed",
2945 [SERVICE_AUTO_RESTART] = "auto-restart",
2948 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2950 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2951 [SERVICE_ONCE] = "once",
2952 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2953 [SERVICE_RESTART_ALWAYS] = "restart-always",
2956 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2958 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2959 [SERVICE_SIMPLE] = "simple",
2960 [SERVICE_FORKING] = "forking",
2961 [SERVICE_ONESHOT] = "oneshot",
2962 [SERVICE_DBUS] = "dbus",
2963 [SERVICE_NOTIFY] = "notify"
2966 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2968 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2969 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2970 [SERVICE_EXEC_START] = "ExecStart",
2971 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2972 [SERVICE_EXEC_RELOAD] = "ExecReload",
2973 [SERVICE_EXEC_STOP] = "ExecStop",
2974 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2977 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2979 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2980 [NOTIFY_NONE] = "none",
2981 [NOTIFY_MAIN] = "main",
2982 [NOTIFY_ALL] = "all"
2985 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2987 const UnitVTable service_vtable = {
2988 .suffix = ".service",
2989 .show_status = true,
2991 .init = service_init,
2992 .done = service_done,
2993 .load = service_load,
2995 .coldplug = service_coldplug,
2997 .dump = service_dump,
2999 .start = service_start,
3000 .stop = service_stop,
3001 .reload = service_reload,
3003 .can_reload = service_can_reload,
3005 .serialize = service_serialize,
3006 .deserialize_item = service_deserialize_item,
3008 .active_state = service_active_state,
3009 .sub_state_to_string = service_sub_state_to_string,
3011 .check_gc = service_check_gc,
3012 .check_snapshot = service_check_snapshot,
3014 .sigchld_event = service_sigchld_event,
3015 .timer_event = service_timer_event,
3017 .reset_failed = service_reset_failed,
3019 .cgroup_notify_empty = service_cgroup_notify_event,
3020 .notify_message = service_notify_message,
3022 .bus_name_owner_change = service_bus_name_owner_change,
3023 .bus_query_pid_done = service_bus_query_pid_done,
3025 .bus_interface = "org.freedesktop.systemd1.Service",
3026 .bus_message_handler = bus_service_message_handler,
3027 .bus_invalidating_properties = bus_service_invalidating_properties,
3029 .enumerate = service_enumerate