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_verify(Service *s) {
830 if (s->meta.load_state != UNIT_LOADED)
833 if (!s->exec_command[SERVICE_EXEC_START]) {
834 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
838 if (s->type != SERVICE_ONESHOT &&
839 s->exec_command[SERVICE_EXEC_START]->command_next) {
840 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
844 if (s->type == SERVICE_DBUS && !s->bus_name) {
845 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
849 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
850 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
857 static int service_add_default_dependencies(Service *s) {
862 /* Add a number of automatic dependencies useful for the
863 * majority of services. */
865 /* First, pull in base system */
866 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
868 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
871 } else if (s->meta.manager->running_as == MANAGER_SESSION) {
873 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
877 /* Second, activate normal shutdown */
878 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
881 static int service_load(Unit *u) {
883 Service *s = SERVICE(u);
887 /* Load a .service file */
888 if ((r = unit_load_fragment(u)) < 0)
891 /* Load a classic init script as a fallback, if we couldn't find anything */
892 if (u->meta.load_state == UNIT_STUB)
893 if ((r = service_load_sysv(s)) < 0)
896 /* Still nothing found? Then let's give up */
897 if (u->meta.load_state == UNIT_STUB)
900 /* We were able to load something, then let's add in the
901 * dropin directories. */
902 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
905 /* This is a new unit? Then let's add in some extras */
906 if (u->meta.load_state == UNIT_LOADED) {
907 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
910 if ((r = unit_add_default_cgroup(u)) < 0)
913 if ((r = sysv_fix_order(s)) < 0)
917 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
920 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
921 s->notify_access = NOTIFY_MAIN;
923 if (s->type == SERVICE_DBUS || s->bus_name)
924 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
927 if (s->meta.default_dependencies)
928 if ((r = service_add_default_dependencies(s)) < 0)
932 return service_verify(s);
935 static void service_dump(Unit *u, FILE *f, const char *prefix) {
937 ServiceExecCommand c;
938 Service *s = SERVICE(u);
944 p2 = strappend(prefix, "\t");
945 prefix2 = p2 ? p2 : prefix;
948 "%sService State: %s\n"
949 "%sPermissionsStartOnly: %s\n"
950 "%sRootDirectoryStartOnly: %s\n"
951 "%sRemainAfterExit: %s\n"
954 "%sNotifyAccess: %s\n",
955 prefix, service_state_to_string(s->state),
956 prefix, yes_no(s->permissions_start_only),
957 prefix, yes_no(s->root_directory_start_only),
958 prefix, yes_no(s->remain_after_exit),
959 prefix, service_type_to_string(s->type),
960 prefix, service_restart_to_string(s->restart),
961 prefix, notify_access_to_string(s->notify_access));
963 if (s->control_pid > 0)
965 "%sControl PID: %lu\n",
966 prefix, (unsigned long) s->control_pid);
971 prefix, (unsigned long) s->main_pid);
976 prefix, s->pid_file);
981 "%sBus Name Good: %s\n",
983 prefix, yes_no(s->bus_name_good));
985 exec_context_dump(&s->exec_context, f, prefix);
987 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
989 if (!s->exec_command[c])
992 fprintf(f, "%s-> %s:\n",
993 prefix, service_exec_command_to_string(c));
995 exec_command_dump_list(s->exec_command[c], f, prefix2);
1000 "%sSysV Init Script Path: %s\n"
1001 "%sSysV Init Script has LSB Header: %s\n",
1002 prefix, s->sysv_path,
1003 prefix, yes_no(s->sysv_has_lsb));
1005 if (s->sysv_start_priority >= 0)
1007 "%sSysVStartPriority: %i\n"
1008 "%sSysVEnabled: %s\n",
1009 prefix, s->sysv_start_priority,
1010 prefix, yes_no(s->sysv_enabled));
1012 if (s->sysv_runlevels)
1013 fprintf(f, "%sSysVRunLevels: %s\n",
1014 prefix, s->sysv_runlevels);
1017 fprintf(f, "%sStatus Text: %s\n",
1018 prefix, s->status_text);
1023 static int service_load_pid_file(Service *s) {
1030 if (s->main_pid_known)
1033 assert(s->main_pid <= 0);
1038 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1041 r = parse_pid(k, &pid);
1047 if (kill(pid, 0) < 0 && errno != EPERM) {
1048 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1049 (unsigned long) pid, s->pid_file);
1053 if ((r = service_set_main_pid(s, pid)) < 0)
1056 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1057 /* FIXME: we need to do something here */
1063 static int service_get_sockets(Service *s, Set **_set) {
1072 if (s->socket_fd >= 0)
1075 /* Collects all Socket objects that belong to this
1076 * service. Note that a service might have multiple sockets
1077 * via multiple names. */
1079 if (!(set = set_new(NULL, NULL)))
1082 SET_FOREACH(t, s->meta.names, i) {
1086 /* Look for all socket objects that go by any of our
1087 * units and collect their fds */
1089 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1094 p = manager_get_unit(s->meta.manager, k);
1100 if ((r = set_put(set, p)) < 0)
1112 static int service_notify_sockets_dead(Service *s) {
1120 if (s->socket_fd >= 0)
1123 /* Notifies all our sockets when we die */
1124 if ((r = service_get_sockets(s, &set)) < 0)
1127 SET_FOREACH(sock, set, i)
1128 socket_notify_service_dead(sock);
1135 static void service_set_state(Service *s, ServiceState state) {
1136 ServiceState old_state;
1139 old_state = s->state;
1142 if (state != SERVICE_START_PRE &&
1143 state != SERVICE_START &&
1144 state != SERVICE_START_POST &&
1145 state != SERVICE_RELOAD &&
1146 state != SERVICE_STOP &&
1147 state != SERVICE_STOP_SIGTERM &&
1148 state != SERVICE_STOP_SIGKILL &&
1149 state != SERVICE_STOP_POST &&
1150 state != SERVICE_FINAL_SIGTERM &&
1151 state != SERVICE_FINAL_SIGKILL &&
1152 state != SERVICE_AUTO_RESTART)
1153 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1155 if (state != SERVICE_START &&
1156 state != SERVICE_START_POST &&
1157 state != SERVICE_RUNNING &&
1158 state != SERVICE_RELOAD &&
1159 state != SERVICE_STOP &&
1160 state != SERVICE_STOP_SIGTERM &&
1161 state != SERVICE_STOP_SIGKILL)
1162 service_unwatch_main_pid(s);
1164 if (state != SERVICE_START_PRE &&
1165 state != SERVICE_START &&
1166 state != SERVICE_START_POST &&
1167 state != SERVICE_RELOAD &&
1168 state != SERVICE_STOP &&
1169 state != SERVICE_STOP_SIGTERM &&
1170 state != SERVICE_STOP_SIGKILL &&
1171 state != SERVICE_STOP_POST &&
1172 state != SERVICE_FINAL_SIGTERM &&
1173 state != SERVICE_FINAL_SIGKILL) {
1174 service_unwatch_control_pid(s);
1175 s->control_command = NULL;
1176 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1179 if (state == SERVICE_DEAD ||
1180 state == SERVICE_STOP ||
1181 state == SERVICE_STOP_SIGTERM ||
1182 state == SERVICE_STOP_SIGKILL ||
1183 state == SERVICE_STOP_POST ||
1184 state == SERVICE_FINAL_SIGTERM ||
1185 state == SERVICE_FINAL_SIGKILL ||
1186 state == SERVICE_FAILED ||
1187 state == SERVICE_AUTO_RESTART)
1188 service_notify_sockets_dead(s);
1190 if (state != SERVICE_START_PRE &&
1191 state != SERVICE_START &&
1192 state != SERVICE_START_POST &&
1193 state != SERVICE_RUNNING &&
1194 state != SERVICE_RELOAD &&
1195 state != SERVICE_STOP &&
1196 state != SERVICE_STOP_SIGTERM &&
1197 state != SERVICE_STOP_SIGKILL &&
1198 state != SERVICE_STOP_POST &&
1199 state != SERVICE_FINAL_SIGTERM &&
1200 state != SERVICE_FINAL_SIGKILL &&
1201 !(state == SERVICE_DEAD && s->meta.job)) {
1202 service_close_socket_fd(s);
1203 service_connection_unref(s);
1206 /* For the inactive states unit_notify() will trim the cgroup,
1207 * but for exit we have to do that ourselves... */
1208 if (state == SERVICE_EXITED)
1209 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1211 if (old_state != state)
1212 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1214 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1217 static int service_coldplug(Unit *u) {
1218 Service *s = SERVICE(u);
1222 assert(s->state == SERVICE_DEAD);
1224 if (s->deserialized_state != s->state) {
1226 if (s->deserialized_state == SERVICE_START_PRE ||
1227 s->deserialized_state == SERVICE_START ||
1228 s->deserialized_state == SERVICE_START_POST ||
1229 s->deserialized_state == SERVICE_RELOAD ||
1230 s->deserialized_state == SERVICE_STOP ||
1231 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1232 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1233 s->deserialized_state == SERVICE_STOP_POST ||
1234 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1235 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1236 s->deserialized_state == SERVICE_AUTO_RESTART) {
1238 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1241 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1243 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1248 if ((s->deserialized_state == SERVICE_START &&
1249 (s->type == SERVICE_FORKING ||
1250 s->type == SERVICE_DBUS ||
1251 s->type == SERVICE_ONESHOT ||
1252 s->type == SERVICE_NOTIFY)) ||
1253 s->deserialized_state == SERVICE_START_POST ||
1254 s->deserialized_state == SERVICE_RUNNING ||
1255 s->deserialized_state == SERVICE_RELOAD ||
1256 s->deserialized_state == SERVICE_STOP ||
1257 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1258 s->deserialized_state == SERVICE_STOP_SIGKILL)
1259 if (s->main_pid > 0)
1260 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1263 if (s->deserialized_state == SERVICE_START_PRE ||
1264 s->deserialized_state == SERVICE_START ||
1265 s->deserialized_state == SERVICE_START_POST ||
1266 s->deserialized_state == SERVICE_RELOAD ||
1267 s->deserialized_state == SERVICE_STOP ||
1268 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1269 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1270 s->deserialized_state == SERVICE_STOP_POST ||
1271 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1272 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1273 if (s->control_pid > 0)
1274 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1277 service_set_state(s, s->deserialized_state);
1283 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1287 unsigned rn_fds = 0;
1295 if (s->socket_fd >= 0)
1298 if ((r = service_get_sockets(s, &set)) < 0)
1301 SET_FOREACH(sock, set, i) {
1305 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1317 if (!(t = new(int, rn_fds+cn_fds))) {
1323 memcpy(t, rfds, rn_fds);
1324 memcpy(t+rn_fds, cfds, cn_fds);
1329 rn_fds = rn_fds+cn_fds;
1347 static int service_spawn(
1352 bool apply_permissions,
1354 bool apply_tty_stdin,
1355 bool set_notify_socket,
1360 int *fds = NULL, *fdsbuf = NULL;
1361 unsigned n_fds = 0, n_env = 0;
1362 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1369 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1370 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1371 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1373 if (s->socket_fd >= 0) {
1374 fds = &s->socket_fd;
1377 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1384 if (timeout && s->timeout_usec) {
1385 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1388 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1390 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1395 if (!(our_env = new0(char*, 3))) {
1400 if (set_notify_socket)
1401 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1406 if (s->main_pid > 0)
1407 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1412 if (!(final_env = strv_env_merge(2,
1413 s->meta.manager->environment,
1428 s->meta.manager->confirm_spawn,
1429 s->meta.cgroup_bondings,
1436 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1437 /* FIXME: we need to do something here */
1443 strv_free(final_env);
1453 strv_free(final_env);
1456 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1461 static int main_pid_good(Service *s) {
1464 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1467 /* If we know the pid file, then lets just check if it is
1469 if (s->main_pid_known)
1470 return s->main_pid > 0;
1472 /* We don't know the pid */
1476 static int control_pid_good(Service *s) {
1479 return s->control_pid > 0;
1482 static int cgroup_good(Service *s) {
1487 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1493 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1500 if (allow_restart &&
1501 !s->forbid_restart &&
1502 (s->restart == SERVICE_RESTART_ALWAYS ||
1503 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1505 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1508 service_set_state(s, SERVICE_AUTO_RESTART);
1510 service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD);
1512 s->forbid_restart = false;
1517 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1518 service_enter_dead(s, false, false);
1521 static void service_enter_signal(Service *s, ServiceState state, bool success);
1523 static void service_enter_stop_post(Service *s, bool success) {
1530 service_unwatch_control_pid(s);
1532 s->control_command_id = SERVICE_EXEC_STOP_POST;
1533 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1534 if ((r = service_spawn(s,
1538 !s->permissions_start_only,
1539 !s->root_directory_start_only,
1542 &s->control_pid)) < 0)
1546 service_set_state(s, SERVICE_STOP_POST);
1548 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1553 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1554 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1557 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1559 Set *pid_set = NULL;
1560 bool wait_for_exit = false;
1567 if (s->exec_context.kill_mode != KILL_NONE) {
1568 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1570 if (s->main_pid > 0) {
1571 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1573 s->main_pid, sig) < 0 && errno != ESRCH)
1575 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1577 wait_for_exit = true;
1580 if (s->control_pid > 0) {
1581 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1583 s->control_pid, sig) < 0 && errno != ESRCH)
1585 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1587 wait_for_exit = true;
1590 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1592 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1597 /* Exclude the main/control pids from being killed via the cgroup */
1598 if (s->main_pid > 0)
1599 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1602 if (s->control_pid > 0)
1603 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1606 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
1607 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1608 log_warning("Failed to kill control group: %s", strerror(-r));
1610 wait_for_exit = true;
1616 if (wait_for_exit) {
1617 if (s->timeout_usec > 0)
1618 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1621 service_set_state(s, state);
1622 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1623 service_enter_stop_post(s, true);
1625 service_enter_dead(s, true, true);
1630 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1632 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1633 service_enter_stop_post(s, false);
1635 service_enter_dead(s, false, true);
1641 static void service_enter_stop(Service *s, bool success) {
1649 service_unwatch_control_pid(s);
1651 s->control_command_id = SERVICE_EXEC_STOP;
1652 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1653 if ((r = service_spawn(s,
1657 !s->permissions_start_only,
1658 !s->root_directory_start_only,
1661 &s->control_pid)) < 0)
1664 service_set_state(s, SERVICE_STOP);
1666 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1671 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1672 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1675 static void service_enter_running(Service *s, bool success) {
1676 int main_pid_ok, cgroup_ok;
1682 main_pid_ok = main_pid_good(s);
1683 cgroup_ok = cgroup_good(s);
1685 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1686 (s->bus_name_good || s->type != SERVICE_DBUS))
1687 service_set_state(s, SERVICE_RUNNING);
1688 else if (s->remain_after_exit)
1689 service_set_state(s, SERVICE_EXITED);
1691 service_enter_stop(s, true);
1694 static void service_enter_start_post(Service *s) {
1698 service_unwatch_control_pid(s);
1700 s->control_command_id = SERVICE_EXEC_START_POST;
1701 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1702 if ((r = service_spawn(s,
1706 !s->permissions_start_only,
1707 !s->root_directory_start_only,
1710 &s->control_pid)) < 0)
1713 service_set_state(s, SERVICE_START_POST);
1715 service_enter_running(s, true);
1720 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1721 service_enter_stop(s, false);
1724 static void service_enter_start(Service *s) {
1730 assert(s->exec_command[SERVICE_EXEC_START]);
1731 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1733 if (s->type == SERVICE_FORKING)
1734 service_unwatch_control_pid(s);
1736 service_unwatch_main_pid(s);
1738 s->control_command_id = SERVICE_EXEC_START;
1739 s->control_command = s->exec_command[SERVICE_EXEC_START];
1741 if ((r = service_spawn(s,
1743 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1748 s->notify_access != NOTIFY_NONE,
1752 if (s->type == SERVICE_SIMPLE) {
1753 /* For simple services we immediately start
1754 * the START_POST binaries. */
1756 service_set_main_pid(s, pid);
1757 service_enter_start_post(s);
1759 } else if (s->type == SERVICE_FORKING) {
1761 /* For forking services we wait until the start
1762 * process exited. */
1764 s->control_pid = pid;
1765 service_set_state(s, SERVICE_START);
1767 } else if (s->type == SERVICE_ONESHOT ||
1768 s->type == SERVICE_DBUS ||
1769 s->type == SERVICE_NOTIFY) {
1771 /* For oneshot services we wait until the start
1772 * process exited, too, but it is our main process. */
1774 /* For D-Bus services we know the main pid right away,
1775 * but wait for the bus name to appear on the
1776 * bus. Notify services are similar. */
1778 service_set_main_pid(s, pid);
1779 service_set_state(s, SERVICE_START);
1781 assert_not_reached("Unknown service type");
1786 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1787 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1790 static void service_enter_start_pre(Service *s) {
1795 service_unwatch_control_pid(s);
1797 s->control_command_id = SERVICE_EXEC_START_PRE;
1798 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1799 if ((r = service_spawn(s,
1803 !s->permissions_start_only,
1804 !s->root_directory_start_only,
1807 &s->control_pid)) < 0)
1810 service_set_state(s, SERVICE_START_PRE);
1812 service_enter_start(s);
1817 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1818 service_enter_dead(s, false, true);
1821 static void service_enter_restart(Service *s) {
1826 dbus_error_init(&error);
1828 service_enter_dead(s, true, false);
1830 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
1833 log_debug("%s scheduled restart job.", s->meta.id);
1837 log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
1838 service_enter_dead(s, false, false);
1840 dbus_error_free(&error);
1843 static void service_enter_reload(Service *s) {
1848 service_unwatch_control_pid(s);
1850 s->control_command_id = SERVICE_EXEC_RELOAD;
1851 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1852 if ((r = service_spawn(s,
1856 !s->permissions_start_only,
1857 !s->root_directory_start_only,
1860 &s->control_pid)) < 0)
1863 service_set_state(s, SERVICE_RELOAD);
1865 service_enter_running(s, true);
1870 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1871 service_enter_stop(s, false);
1874 static void service_run_next_control(Service *s, bool success) {
1878 assert(s->control_command);
1879 assert(s->control_command->command_next);
1884 assert(s->control_command_id != SERVICE_EXEC_START);
1886 s->control_command = s->control_command->command_next;
1887 service_unwatch_control_pid(s);
1889 if ((r = service_spawn(s,
1893 !s->permissions_start_only,
1894 !s->root_directory_start_only,
1895 s->control_command_id == SERVICE_EXEC_START_PRE ||
1896 s->control_command_id == SERVICE_EXEC_STOP_POST,
1898 &s->control_pid)) < 0)
1904 log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
1906 if (s->state == SERVICE_START_PRE)
1907 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1908 else if (s->state == SERVICE_STOP)
1909 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1910 else if (s->state == SERVICE_STOP_POST)
1911 service_enter_dead(s, false, true);
1913 service_enter_stop(s, false);
1916 static void service_run_next_main(Service *s, bool success) {
1921 assert(s->control_command);
1922 assert(s->control_command->command_next);
1927 assert(s->control_command_id == SERVICE_EXEC_START);
1928 assert(s->type == SERVICE_ONESHOT);
1930 s->control_command = s->control_command->command_next;
1931 service_unwatch_main_pid(s);
1933 if ((r = service_spawn(s,
1940 s->notify_access != NOTIFY_NONE,
1944 service_set_main_pid(s, pid);
1949 log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
1950 service_enter_stop(s, false);
1953 static int service_start(Unit *u) {
1954 Service *s = SERVICE(u);
1958 /* We cannot fulfill this request right now, try again later
1960 if (s->state == SERVICE_STOP ||
1961 s->state == SERVICE_STOP_SIGTERM ||
1962 s->state == SERVICE_STOP_SIGKILL ||
1963 s->state == SERVICE_STOP_POST ||
1964 s->state == SERVICE_FINAL_SIGTERM ||
1965 s->state == SERVICE_FINAL_SIGKILL)
1968 /* Already on it! */
1969 if (s->state == SERVICE_START_PRE ||
1970 s->state == SERVICE_START ||
1971 s->state == SERVICE_START_POST)
1974 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
1976 /* Make sure we don't enter a busy loop of some kind. */
1977 if (!ratelimit_test(&s->ratelimit)) {
1978 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1982 if ((s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1983 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1984 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) &&
1986 log_warning("%s can only be started with a per-connection socket.", u->meta.id);
1991 s->main_pid_known = false;
1992 s->forbid_restart = false;
1994 service_enter_start_pre(s);
1998 static int service_stop(Unit *u) {
1999 Service *s = SERVICE(u);
2003 /* This is a user request, so don't do restarts on this
2005 s->forbid_restart = true;
2008 if (s->state == SERVICE_STOP ||
2009 s->state == SERVICE_STOP_SIGTERM ||
2010 s->state == SERVICE_STOP_SIGKILL ||
2011 s->state == SERVICE_STOP_POST ||
2012 s->state == SERVICE_FINAL_SIGTERM ||
2013 s->state == SERVICE_FINAL_SIGKILL)
2016 /* Don't allow a restart */
2017 if (s->state == SERVICE_AUTO_RESTART) {
2018 service_set_state(s, SERVICE_DEAD);
2022 /* If there's already something running we go directly into
2024 if (s->state == SERVICE_START_PRE ||
2025 s->state == SERVICE_START ||
2026 s->state == SERVICE_START_POST ||
2027 s->state == SERVICE_RELOAD) {
2028 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2032 assert(s->state == SERVICE_RUNNING ||
2033 s->state == SERVICE_EXITED);
2035 service_enter_stop(s, true);
2039 static int service_reload(Unit *u) {
2040 Service *s = SERVICE(u);
2044 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2046 service_enter_reload(s);
2050 static bool service_can_reload(Unit *u) {
2051 Service *s = SERVICE(u);
2055 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2058 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2059 Service *s = SERVICE(u);
2065 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2066 unit_serialize_item(u, f, "failure", yes_no(s->failure));
2068 if (s->control_pid > 0)
2069 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2071 if (s->main_pid_known && s->main_pid > 0)
2072 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2074 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2077 unit_serialize_item(u, f, "status-text", s->status_text);
2079 /* There's a minor uncleanliness here: if there are multiple
2080 * commands attached here, we will start from the first one
2082 if (s->control_command_id >= 0)
2083 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2085 if (s->socket_fd >= 0) {
2088 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2091 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2094 if (s->main_exec_status.pid > 0) {
2095 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2097 if (s->main_exec_status.start_timestamp.realtime > 0) {
2098 unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2099 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2101 unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2102 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2105 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2106 unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2107 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2108 unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2109 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2111 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2112 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2119 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2120 Service *s = SERVICE(u);
2127 if (streq(key, "state")) {
2130 if ((state = service_state_from_string(value)) < 0)
2131 log_debug("Failed to parse state value %s", value);
2133 s->deserialized_state = state;
2134 } else if (streq(key, "failure")) {
2137 if ((b = parse_boolean(value)) < 0)
2138 log_debug("Failed to parse failure value %s", value);
2140 s->failure = b || s->failure;
2141 } else if (streq(key, "control-pid")) {
2144 if (parse_pid(value, &pid) < 0)
2145 log_debug("Failed to parse control-pid value %s", value);
2147 s->control_pid = pid;
2148 } else if (streq(key, "main-pid")) {
2151 if (parse_pid(value, &pid) < 0)
2152 log_debug("Failed to parse main-pid value %s", value);
2154 service_set_main_pid(s, (pid_t) pid);
2155 } else if (streq(key, "main-pid-known")) {
2158 if ((b = parse_boolean(value)) < 0)
2159 log_debug("Failed to parse main-pid-known value %s", value);
2161 s->main_pid_known = b;
2162 } else if (streq(key, "status-text")) {
2165 if ((t = strdup(value))) {
2166 free(s->status_text);
2170 } else if (streq(key, "control-command")) {
2171 ServiceExecCommand id;
2173 if ((id = service_exec_command_from_string(value)) < 0)
2174 log_debug("Failed to parse exec-command value %s", value);
2176 s->control_command_id = id;
2177 s->control_command = s->exec_command[id];
2179 } else if (streq(key, "socket-fd")) {
2182 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2183 log_debug("Failed to parse socket-fd value %s", value);
2186 if (s->socket_fd >= 0)
2187 close_nointr_nofail(s->socket_fd);
2188 s->socket_fd = fdset_remove(fds, fd);
2190 } else if (streq(key, "main-exec-status-pid")) {
2193 if (parse_pid(value, &pid) < 0)
2194 log_debug("Failed to parse main-exec-status-pid value %s", value);
2196 s->main_exec_status.pid = pid;
2197 } else if (streq(key, "main-exec-status-code")) {
2200 if (safe_atoi(value, &i) < 0)
2201 log_debug("Failed to parse main-exec-status-code value %s", value);
2203 s->main_exec_status.code = i;
2204 } else if (streq(key, "main-exec-status-status")) {
2207 if (safe_atoi(value, &i) < 0)
2208 log_debug("Failed to parse main-exec-status-status value %s", value);
2210 s->main_exec_status.status = i;
2211 } else if (streq(key, "main-exec-status-start-realtime")) {
2214 if (safe_atou64(value, &k) < 0)
2215 log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2217 s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2218 } else if (streq(key, "main-exec-status-start-monotonic")) {
2221 if (safe_atou64(value, &k) < 0)
2222 log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2224 s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2225 } else if (streq(key, "main-exec-status-exit-realtime")) {
2228 if (safe_atou64(value, &k) < 0)
2229 log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2231 s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2232 } else if (streq(key, "main-exec-status-exit-monotonic")) {
2235 if (safe_atou64(value, &k) < 0)
2236 log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2238 s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2240 log_debug("Unknown serialization key '%s'", key);
2245 static UnitActiveState service_active_state(Unit *u) {
2248 return state_translation_table[SERVICE(u)->state];
2251 static const char *service_sub_state_to_string(Unit *u) {
2254 return service_state_to_string(SERVICE(u)->state);
2257 static bool service_check_gc(Unit *u) {
2258 Service *s = SERVICE(u);
2262 return !!s->sysv_path;
2265 static bool service_check_snapshot(Unit *u) {
2266 Service *s = SERVICE(u);
2270 return !s->got_socket_fd;
2273 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2274 Service *s = SERVICE(u);
2281 success = is_clean_exit_lsb(code, status);
2283 success = is_clean_exit(code, status);
2285 if (s->main_pid == pid) {
2288 exec_status_exit(&s->main_exec_status, pid, code, status);
2290 if (s->type != SERVICE_FORKING && s->control_command) {
2291 s->control_command->exec_status = s->main_exec_status;
2293 if (s->control_command->ignore)
2297 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2298 "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2299 s->failure = s->failure || !success;
2301 if (s->control_command &&
2302 s->control_command->command_next &&
2305 /* There is another command to *
2306 * execute, so let's do that. */
2308 log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2309 service_run_next_main(s, success);
2313 /* The service exited, so the service is officially
2316 s->control_command = NULL;
2317 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2321 case SERVICE_START_POST:
2322 case SERVICE_RELOAD:
2324 /* Need to wait until the operation is
2329 if (s->type == SERVICE_ONESHOT) {
2330 /* This was our main goal, so let's go on */
2332 service_enter_start_post(s);
2334 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2337 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2342 case SERVICE_RUNNING:
2343 service_enter_running(s, success);
2346 case SERVICE_STOP_SIGTERM:
2347 case SERVICE_STOP_SIGKILL:
2349 if (!control_pid_good(s))
2350 service_enter_stop_post(s, success);
2352 /* If there is still a control process, wait for that first */
2356 assert_not_reached("Uh, main process died at wrong time.");
2360 } else if (s->control_pid == pid) {
2364 if (s->control_command) {
2365 exec_status_exit(&s->control_command->exec_status, pid, code, status);
2367 if (s->control_command->ignore)
2371 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2372 "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2373 s->failure = s->failure || !success;
2375 if (s->control_command &&
2376 s->control_command->command_next &&
2379 /* There is another command to *
2380 * execute, so let's do that. */
2382 log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2383 service_run_next_control(s, success);
2386 /* No further commands for this step, so let's
2387 * figure out what to do next */
2389 s->control_command = NULL;
2390 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2392 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2396 case SERVICE_START_PRE:
2398 service_enter_start(s);
2400 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2404 assert(s->type == SERVICE_FORKING);
2406 /* Let's try to load the pid
2407 * file here if we can. We
2408 * ignore the return value,
2409 * since the PID file might
2410 * actually be created by a
2411 * START_POST script */
2415 service_load_pid_file(s);
2417 service_enter_start_post(s);
2419 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2423 case SERVICE_START_POST:
2424 if (success && s->pid_file && !s->main_pid_known) {
2427 /* Hmm, let's see if we can
2428 * load the pid now after the
2429 * start-post scripts got
2432 if ((r = service_load_pid_file(s)) < 0)
2433 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2438 case SERVICE_RELOAD:
2440 service_enter_running(s, true);
2442 service_enter_stop(s, false);
2447 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2450 case SERVICE_STOP_SIGTERM:
2451 case SERVICE_STOP_SIGKILL:
2452 if (main_pid_good(s) <= 0)
2453 service_enter_stop_post(s, success);
2455 /* If there is still a service
2456 * process around, wait until
2457 * that one quit, too */
2460 case SERVICE_STOP_POST:
2461 case SERVICE_FINAL_SIGTERM:
2462 case SERVICE_FINAL_SIGKILL:
2463 service_enter_dead(s, success, true);
2467 assert_not_reached("Uh, control process died at wrong time.");
2472 /* Notify clients about changed exit status */
2473 unit_add_to_dbus_queue(u);
2476 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2477 Service *s = SERVICE(u);
2480 assert(elapsed == 1);
2482 assert(w == &s->timer_watch);
2486 case SERVICE_START_PRE:
2488 log_warning("%s operation timed out. Terminating.", u->meta.id);
2489 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2492 case SERVICE_START_POST:
2493 case SERVICE_RELOAD:
2494 log_warning("%s operation timed out. Stopping.", u->meta.id);
2495 service_enter_stop(s, false);
2499 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2500 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2503 case SERVICE_STOP_SIGTERM:
2504 log_warning("%s stopping timed out. Killing.", u->meta.id);
2505 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2508 case SERVICE_STOP_SIGKILL:
2509 /* Uh, wie sent a SIGKILL and it is still not gone?
2510 * Must be something we cannot kill, so let's just be
2511 * weirded out and continue */
2513 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2514 service_enter_stop_post(s, false);
2517 case SERVICE_STOP_POST:
2518 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2519 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2522 case SERVICE_FINAL_SIGTERM:
2523 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2524 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2527 case SERVICE_FINAL_SIGKILL:
2528 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
2529 service_enter_dead(s, false, true);
2532 case SERVICE_AUTO_RESTART:
2533 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2534 service_enter_restart(s);
2538 assert_not_reached("Timeout at wrong time.");
2542 static void service_cgroup_notify_event(Unit *u) {
2543 Service *s = SERVICE(u);
2547 log_debug("%s: cgroup is empty", u->meta.id);
2551 /* Waiting for SIGCHLD is usually more interesting,
2552 * because it includes return codes/signals. Which is
2553 * why we ignore the cgroup events for most cases,
2554 * except when we don't know pid which to expect the
2557 case SERVICE_RUNNING:
2558 service_enter_running(s, true);
2561 case SERVICE_STOP_SIGTERM:
2562 case SERVICE_STOP_SIGKILL:
2563 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2564 service_enter_stop_post(s, true);
2568 case SERVICE_FINAL_SIGTERM:
2569 case SERVICE_FINAL_SIGKILL:
2570 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2571 service_enter_dead(s, true, true);
2580 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2581 Service *s = SERVICE(u);
2586 if (s->notify_access == NOTIFY_NONE) {
2587 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2588 u->meta.id, (unsigned long) pid);
2592 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2593 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2594 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2598 log_debug("%s: Got message", u->meta.id);
2600 /* Interpret MAINPID= */
2601 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2602 (s->state == SERVICE_START ||
2603 s->state == SERVICE_START_POST ||
2604 s->state == SERVICE_RUNNING ||
2605 s->state == SERVICE_RELOAD)) {
2607 if (parse_pid(e + 8, &pid) < 0)
2608 log_warning("Failed to parse notification message %s", e);
2610 log_debug("%s: got %s", u->meta.id, e);
2611 service_set_main_pid(s, pid);
2615 /* Interpret READY= */
2616 if (s->type == SERVICE_NOTIFY &&
2617 s->state == SERVICE_START &&
2618 strv_find(tags, "READY=1")) {
2619 log_debug("%s: got READY=1", u->meta.id);
2621 service_enter_start_post(s);
2624 /* Interpret STATUS= */
2625 if ((e = strv_find_prefix(tags, "STATUS="))) {
2629 if (!(t = strdup(e+7))) {
2630 log_error("Failed to allocate string.");
2634 log_debug("%s: got %s", u->meta.id, e);
2636 free(s->status_text);
2639 free(s->status_text);
2640 s->status_text = NULL;
2645 /* Notify clients about changed status or main pid */
2646 unit_add_to_dbus_queue(u);
2649 static int service_enumerate(Manager *m) {
2653 char *path = NULL, *fpath = NULL, *name = NULL;
2654 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
2661 zero(runlevel_services);
2663 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2664 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2669 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2677 if (!(d = opendir(path))) {
2678 if (errno != ENOENT)
2679 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2684 while ((de = readdir(d))) {
2687 if (ignore_file(de->d_name))
2690 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2693 if (strlen(de->d_name) < 4)
2696 a = undecchar(de->d_name[1]);
2697 b = undecchar(de->d_name[2]);
2704 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2709 if (access(fpath, X_OK) < 0) {
2711 if (errno != ENOENT)
2712 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2718 if (!(name = sysv_translate_name(de->d_name + 3))) {
2723 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2724 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2728 if (de->d_name[0] == 'S') {
2730 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
2731 SERVICE(service)->sysv_start_priority =
2732 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2734 SERVICE(service)->sysv_enabled = true;
2737 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
2740 if ((r = set_put(runlevel_services[i], service)) < 0)
2743 } else if (de->d_name[0] == 'K' &&
2744 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2745 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2747 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
2750 if ((r = set_put(shutdown_services, service)) < 0)
2756 /* Now we loaded all stubs and are aware of the lowest
2757 start-up priority for all services, not let's actually load
2758 the services, this will also tell us which services are
2759 actually native now */
2760 manager_dispatch_load_queue(m);
2762 /* If this is a native service, rely on native ways to pull in
2763 * a service, don't pull it in via sysv rcN.d links. */
2764 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
2765 SET_FOREACH(service, runlevel_services[i], j) {
2766 service = unit_follow_merge(service);
2768 if (service->meta.fragment_path)
2771 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2775 /* We honour K links only for halt/reboot. For the normal
2776 * runlevels we assume the stop jobs will be implicitly added
2777 * by the core logic. Also, we don't really distuingish here
2778 * between the runlevels 0 and 6 and just add them to the
2779 * special shutdown target. On SUSE the boot.d/ runlevel is
2780 * also used for shutdown, so we add links for that too to the
2781 * shutdown target.*/
2782 SET_FOREACH(service, shutdown_services, j) {
2783 service = unit_follow_merge(service);
2785 if (service->meta.fragment_path)
2788 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2799 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
2800 set_free(runlevel_services[i]);
2801 set_free(shutdown_services);
2809 static void service_bus_name_owner_change(
2812 const char *old_owner,
2813 const char *new_owner) {
2815 Service *s = SERVICE(u);
2820 assert(streq(s->bus_name, name));
2821 assert(old_owner || new_owner);
2823 if (old_owner && new_owner)
2824 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2826 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2828 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2830 s->bus_name_good = !!new_owner;
2832 if (s->type == SERVICE_DBUS) {
2834 /* service_enter_running() will figure out what to
2836 if (s->state == SERVICE_RUNNING)
2837 service_enter_running(s, true);
2838 else if (s->state == SERVICE_START && new_owner)
2839 service_enter_start_post(s);
2841 } else if (new_owner &&
2843 (s->state == SERVICE_START ||
2844 s->state == SERVICE_START_POST ||
2845 s->state == SERVICE_RUNNING ||
2846 s->state == SERVICE_RELOAD)) {
2848 /* Try to acquire PID from bus service */
2849 log_debug("Trying to acquire PID from D-Bus name...");
2851 bus_query_pid(u->meta.manager, name);
2855 static void service_bus_query_pid_done(
2860 Service *s = SERVICE(u);
2865 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2867 if (s->main_pid <= 0 &&
2868 (s->state == SERVICE_START ||
2869 s->state == SERVICE_START_POST ||
2870 s->state == SERVICE_RUNNING ||
2871 s->state == SERVICE_RELOAD))
2872 service_set_main_pid(s, pid);
2875 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2879 /* This is called by the socket code when instantiating a new
2880 * service for a stream socket and the socket needs to be
2883 if (s->meta.load_state != UNIT_LOADED)
2886 if (s->socket_fd >= 0)
2889 if (s->state != SERVICE_DEAD)
2893 s->got_socket_fd = true;
2899 static void service_reset_failed(Unit *u) {
2900 Service *s = SERVICE(u);
2904 if (s->state == SERVICE_FAILED)
2905 service_set_state(s, SERVICE_DEAD);
2910 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2911 [SERVICE_DEAD] = "dead",
2912 [SERVICE_START_PRE] = "start-pre",
2913 [SERVICE_START] = "start",
2914 [SERVICE_START_POST] = "start-post",
2915 [SERVICE_RUNNING] = "running",
2916 [SERVICE_EXITED] = "exited",
2917 [SERVICE_RELOAD] = "reload",
2918 [SERVICE_STOP] = "stop",
2919 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2920 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2921 [SERVICE_STOP_POST] = "stop-post",
2922 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2923 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2924 [SERVICE_FAILED] = "failed",
2925 [SERVICE_AUTO_RESTART] = "auto-restart",
2928 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2930 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2931 [SERVICE_ONCE] = "once",
2932 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2933 [SERVICE_RESTART_ALWAYS] = "restart-always",
2936 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2938 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2939 [SERVICE_SIMPLE] = "simple",
2940 [SERVICE_FORKING] = "forking",
2941 [SERVICE_ONESHOT] = "oneshot",
2942 [SERVICE_DBUS] = "dbus",
2943 [SERVICE_NOTIFY] = "notify"
2946 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2948 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2949 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2950 [SERVICE_EXEC_START] = "ExecStart",
2951 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2952 [SERVICE_EXEC_RELOAD] = "ExecReload",
2953 [SERVICE_EXEC_STOP] = "ExecStop",
2954 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2957 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2959 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2960 [NOTIFY_NONE] = "none",
2961 [NOTIFY_MAIN] = "main",
2962 [NOTIFY_ALL] = "all"
2965 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2967 const UnitVTable service_vtable = {
2968 .suffix = ".service",
2969 .show_status = true,
2971 .init = service_init,
2972 .done = service_done,
2973 .load = service_load,
2975 .coldplug = service_coldplug,
2977 .dump = service_dump,
2979 .start = service_start,
2980 .stop = service_stop,
2981 .reload = service_reload,
2983 .can_reload = service_can_reload,
2985 .serialize = service_serialize,
2986 .deserialize_item = service_deserialize_item,
2988 .active_state = service_active_state,
2989 .sub_state_to_string = service_sub_state_to_string,
2991 .check_gc = service_check_gc,
2992 .check_snapshot = service_check_snapshot,
2994 .sigchld_event = service_sigchld_event,
2995 .timer_event = service_timer_event,
2997 .reset_failed = service_reset_failed,
2999 .cgroup_notify_empty = service_cgroup_notify_event,
3000 .notify_message = service_notify_message,
3002 .bus_name_owner_change = service_bus_name_owner_change,
3003 .bus_query_pid_done = service_bus_query_pid_done,
3005 .bus_interface = "org.freedesktop.systemd1.Service",
3006 .bus_message_handler = bus_service_message_handler,
3007 .bus_invalidating_properties = bus_service_invalidating_properties,
3009 .enumerate = service_enumerate