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 else if (startswith(name, "@"))
236 /* Drop Arch-style background prefix */
237 strcpy(stpcpy(r, name + 1), ".service");
240 /* Normal init scripts */
241 strcpy(stpcpy(r, name), ".service");
246 static int sysv_translate_facility(const char *name, char **_r) {
248 static const char * const table[] = {
249 /* LSB defined facilities */
250 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
251 "$network", SPECIAL_NETWORK_TARGET,
252 "$named", SPECIAL_NSS_LOOKUP_TARGET,
253 "$portmap", SPECIAL_RPCBIND_TARGET,
254 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
255 "$syslog", SPECIAL_SYSLOG_TARGET,
256 "$time", SPECIAL_RTC_SET_TARGET,
258 /* Debian extensions */
260 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
262 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
263 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
266 /* Fedora extensions, lacking the $ prefix */
267 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
268 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
269 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
276 for (i = 0; i < ELEMENTSOF(table); i += 2)
277 if (streq(table[i], name)) {
278 if (!(r = strdup(table[i+1])))
285 r = unit_name_build(name+1, NULL, ".target");
287 r = sysv_translate_name(name);
300 static int sysv_fix_order(Service *s) {
306 if (s->sysv_start_priority < 0)
309 /* For each pair of services where at least one lacks a LSB
310 * header, we use the start priority value to order things. */
312 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
315 bool special_s, special_t;
317 t = (Service*) other;
322 if (t->sysv_start_priority < 0)
325 /* If both units have modern headers we don't care
326 * about the priorities */
327 if ((s->meta.fragment_path || s->sysv_has_lsb) &&
328 (t->meta.fragment_path || t->sysv_has_lsb))
331 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
332 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
334 if (special_t && !special_s)
336 else if (special_s && !special_t)
338 else if (t->sysv_start_priority < s->sysv_start_priority)
340 else if (t->sysv_start_priority > s->sysv_start_priority)
345 /* FIXME: Maybe we should compare the name here lexicographically? */
347 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
354 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
357 if (!(c = new0(ExecCommand, 1)))
360 if (!(c->path = strdup(path))) {
365 if (!(c->argv = strv_new(path, arg1, NULL))) {
374 static int sysv_exec_commands(Service *s) {
378 assert(s->sysv_path);
380 if (!(c = exec_command_new(s->sysv_path, "start")))
382 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
384 if (!(c = exec_command_new(s->sysv_path, "stop")))
386 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
388 if (!(c = exec_command_new(s->sysv_path, "reload")))
390 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
395 static int service_load_sysv_path(Service *s, const char *path) {
412 if (!(f = fopen(path, "re"))) {
413 r = errno == ENOENT ? 0 : -errno;
418 if (!(s->sysv_path = strdup(path))) {
424 char l[LINE_MAX], *t;
426 if (!fgets(l, sizeof(l), f)) {
431 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
441 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
443 s->sysv_has_lsb = true;
447 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
453 t += strspn(t, WHITESPACE);
455 if (state == NORMAL) {
457 /* Try to parse Red Hat style chkconfig headers */
459 if (startswith_no_case(t, "chkconfig:")) {
461 char runlevels[16], *k;
465 if (sscanf(t+10, "%15s %i %*i",
467 &start_priority) != 2) {
469 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
473 /* A start priority gathered from the
474 * symlink farms is preferred over the
475 * data from the LSB header. */
476 if (start_priority < 0 || start_priority > 99)
477 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
478 else if (s->sysv_start_priority < 0)
479 s->sysv_start_priority = start_priority;
481 char_array_0(runlevels);
482 k = delete_chars(runlevels, WHITESPACE "-");
487 if (!(d = strdup(k))) {
492 free(s->sysv_runlevels);
493 s->sysv_runlevels = d;
496 } else if (startswith_no_case(t, "description:") &&
497 !u->meta.description) {
499 size_t k = strlen(t);
502 if (t[k-1] == '\\') {
507 if (!(d = strappend("LSB: ", strstrip(t+12)))) {
512 free(u->meta.description);
513 u->meta.description = d;
515 } else if (startswith_no_case(t, "pidfile:")) {
522 if (!path_is_absolute(fn)) {
523 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
527 if (!(fn = strdup(fn))) {
536 } else if (state == DESCRIPTION) {
538 /* Try to parse Red Hat style description
541 size_t k = strlen(t);
549 assert(u->meta.description);
550 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
555 free(u->meta.description);
556 u->meta.description = d;
558 } else if (state == LSB || state == LSB_DESCRIPTION) {
560 if (startswith_no_case(t, "Provides:")) {
566 FOREACH_WORD_QUOTED(w, z, t+9, i) {
569 if (!(n = strndup(w, z))) {
574 r = sysv_translate_facility(n, &m);
583 if (unit_name_to_type(m) == UNIT_SERVICE)
584 r = unit_add_name(u, m);
586 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
588 if (s->sysv_enabled) {
591 if ((k = unit_add_dependency_by_name_inverse(u, UNIT_WANTS, m, NULL, true)) < 0)
597 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
602 } else if (startswith_no_case(t, "Required-Start:") ||
603 startswith_no_case(t, "Should-Start:") ||
604 startswith_no_case(t, "X-Start-Before:") ||
605 startswith_no_case(t, "X-Start-After:")) {
611 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
614 if (!(n = strndup(w, z))) {
619 r = sysv_translate_facility(n, &m);
628 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
631 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
635 } else if (startswith_no_case(t, "Default-Start:")) {
640 k = delete_chars(t+14, WHITESPACE "-");
643 if (!(d = strdup(k))) {
648 free(s->sysv_runlevels);
649 s->sysv_runlevels = d;
652 } else if (startswith_no_case(t, "Description:") &&
653 !u->meta.description) {
656 /* We use the long description only if
657 * no short description is set. */
659 state = LSB_DESCRIPTION;
661 if (!(d = strappend("LSB: ", strstrip(t+12)))) {
666 free(u->meta.description);
667 u->meta.description = d;
669 } else if (startswith_no_case(t, "Short-Description:")) {
674 if (!(d = strappend("LSB: ", strstrip(t+18)))) {
679 free(u->meta.description);
680 u->meta.description = d;
682 } else if (startswith_no_case(t, "X-Interactive:")) {
685 if ((b = parse_boolean(strstrip(t+14))) < 0) {
686 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
691 s->exec_context.std_input = EXEC_INPUT_TTY;
693 s->exec_context.std_input = EXEC_INPUT_NULL;
695 } else if (state == LSB_DESCRIPTION) {
697 if (startswith(l, "#\t") || startswith(l, "# ")) {
700 assert(u->meta.description);
701 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
706 free(u->meta.description);
707 u->meta.description = d;
714 if ((r = sysv_exec_commands(s)) < 0)
717 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
718 /* If there a runlevels configured for this service
719 * but none of the standard ones, then we assume this
720 * is some special kind of service (which might be
721 * needed for early boot) and don't create any links
724 s->meta.default_dependencies = false;
726 /* Don't timeout special services during boot (like fsck) */
730 /* Special setting for all SysV services */
731 s->type = SERVICE_FORKING;
732 s->remain_after_exit = true;
733 s->restart = SERVICE_ONCE;
734 s->exec_context.std_output =
735 (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY)
736 ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
737 s->exec_context.kill_mode = KILL_PROCESS_GROUP;
739 u->meta.load_state = UNIT_LOADED;
750 static int service_load_sysv_name(Service *s, const char *name) {
756 /* For SysV services we strip the boot. or .sh
757 * prefixes/suffixes. */
758 if (startswith(name, "boot.") ||
759 endswith(name, ".sh.service"))
762 STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
766 if (asprintf(&path, "%s/%s", *p, name) < 0)
769 assert(endswith(path, ".service"));
770 path[strlen(path)-8] = 0;
772 r = service_load_sysv_path(s, path);
774 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
775 /* Try Debian style xxx.sh source'able init scripts */
777 r = service_load_sysv_path(s, path);
782 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
783 /* Try SUSE style boot.xxx init scripts */
785 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
788 path[strlen(path)-8] = 0;
789 r = service_load_sysv_path(s, path);
796 if ((s->meta.load_state != UNIT_STUB))
803 static int service_load_sysv(Service *s) {
810 /* Load service data from SysV init scripts, preferably with
813 if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
816 if ((t = s->meta.id))
817 if ((r = service_load_sysv_name(s, t)) < 0)
820 if (s->meta.load_state == UNIT_STUB)
821 SET_FOREACH(t, s->meta.names, i) {
825 if ((r = service_load_sysv_name(s, t)) < 0)
828 if (s->meta.load_state != UNIT_STUB)
835 static int service_verify(Service *s) {
838 if (s->meta.load_state != UNIT_LOADED)
841 if (!s->exec_command[SERVICE_EXEC_START]) {
842 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
846 if (s->type != SERVICE_ONESHOT &&
847 s->exec_command[SERVICE_EXEC_START]->command_next) {
848 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
852 if (s->type == SERVICE_DBUS && !s->bus_name) {
853 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
857 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
858 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
865 static int service_add_default_dependencies(Service *s) {
870 /* Add a number of automatic dependencies useful for the
871 * majority of services. */
873 /* First, pull in base system */
874 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
876 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
879 } else if (s->meta.manager->running_as == MANAGER_SESSION) {
881 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
885 /* Second, activate normal shutdown */
886 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
889 static int service_load(Unit *u) {
891 Service *s = SERVICE(u);
895 /* Load a .service file */
896 if ((r = unit_load_fragment(u)) < 0)
899 /* Load a classic init script as a fallback, if we couldn't find anything */
900 if (u->meta.load_state == UNIT_STUB)
901 if ((r = service_load_sysv(s)) < 0)
904 /* Still nothing found? Then let's give up */
905 if (u->meta.load_state == UNIT_STUB)
908 /* We were able to load something, then let's add in the
909 * dropin directories. */
910 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
913 /* This is a new unit? Then let's add in some extras */
914 if (u->meta.load_state == UNIT_LOADED) {
915 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
918 if ((r = unit_add_default_cgroup(u)) < 0)
921 if ((r = sysv_fix_order(s)) < 0)
925 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
928 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
929 s->notify_access = NOTIFY_MAIN;
931 if (s->type == SERVICE_DBUS || s->bus_name)
932 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
935 if (s->meta.default_dependencies)
936 if ((r = service_add_default_dependencies(s)) < 0)
940 return service_verify(s);
943 static void service_dump(Unit *u, FILE *f, const char *prefix) {
945 ServiceExecCommand c;
946 Service *s = SERVICE(u);
952 p2 = strappend(prefix, "\t");
953 prefix2 = p2 ? p2 : prefix;
956 "%sService State: %s\n"
957 "%sPermissionsStartOnly: %s\n"
958 "%sRootDirectoryStartOnly: %s\n"
959 "%sRemainAfterExit: %s\n"
962 "%sNotifyAccess: %s\n",
963 prefix, service_state_to_string(s->state),
964 prefix, yes_no(s->permissions_start_only),
965 prefix, yes_no(s->root_directory_start_only),
966 prefix, yes_no(s->remain_after_exit),
967 prefix, service_type_to_string(s->type),
968 prefix, service_restart_to_string(s->restart),
969 prefix, notify_access_to_string(s->notify_access));
971 if (s->control_pid > 0)
973 "%sControl PID: %lu\n",
974 prefix, (unsigned long) s->control_pid);
979 prefix, (unsigned long) s->main_pid);
984 prefix, s->pid_file);
989 "%sBus Name Good: %s\n",
991 prefix, yes_no(s->bus_name_good));
993 exec_context_dump(&s->exec_context, f, prefix);
995 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
997 if (!s->exec_command[c])
1000 fprintf(f, "%s-> %s:\n",
1001 prefix, service_exec_command_to_string(c));
1003 exec_command_dump_list(s->exec_command[c], f, prefix2);
1008 "%sSysV Init Script Path: %s\n"
1009 "%sSysV Init Script has LSB Header: %s\n",
1010 prefix, s->sysv_path,
1011 prefix, yes_no(s->sysv_has_lsb));
1013 if (s->sysv_start_priority >= 0)
1015 "%sSysVStartPriority: %i\n"
1016 "%sSysVEnabled: %s\n",
1017 prefix, s->sysv_start_priority,
1018 prefix, yes_no(s->sysv_enabled));
1020 if (s->sysv_runlevels)
1021 fprintf(f, "%sSysVRunLevels: %s\n",
1022 prefix, s->sysv_runlevels);
1025 fprintf(f, "%sStatus Text: %s\n",
1026 prefix, s->status_text);
1031 static int service_load_pid_file(Service *s) {
1038 if (s->main_pid_known)
1041 assert(s->main_pid <= 0);
1046 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1049 r = parse_pid(k, &pid);
1055 if (kill(pid, 0) < 0 && errno != EPERM) {
1056 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1057 (unsigned long) pid, s->pid_file);
1061 if ((r = service_set_main_pid(s, pid)) < 0)
1064 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1065 /* FIXME: we need to do something here */
1071 static int service_get_sockets(Service *s, Set **_set) {
1080 if (s->socket_fd >= 0)
1083 /* Collects all Socket objects that belong to this
1084 * service. Note that a service might have multiple sockets
1085 * via multiple names. */
1087 if (!(set = set_new(NULL, NULL)))
1090 SET_FOREACH(t, s->meta.names, i) {
1094 /* Look for all socket objects that go by any of our
1095 * units and collect their fds */
1097 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1102 p = manager_get_unit(s->meta.manager, k);
1108 if ((r = set_put(set, p)) < 0)
1120 static int service_notify_sockets_dead(Service *s) {
1128 if (s->socket_fd >= 0)
1131 /* Notifies all our sockets when we die */
1132 if ((r = service_get_sockets(s, &set)) < 0)
1135 SET_FOREACH(sock, set, i)
1136 socket_notify_service_dead(sock);
1143 static void service_set_state(Service *s, ServiceState state) {
1144 ServiceState old_state;
1147 old_state = s->state;
1150 if (state != SERVICE_START_PRE &&
1151 state != SERVICE_START &&
1152 state != SERVICE_START_POST &&
1153 state != SERVICE_RELOAD &&
1154 state != SERVICE_STOP &&
1155 state != SERVICE_STOP_SIGTERM &&
1156 state != SERVICE_STOP_SIGKILL &&
1157 state != SERVICE_STOP_POST &&
1158 state != SERVICE_FINAL_SIGTERM &&
1159 state != SERVICE_FINAL_SIGKILL &&
1160 state != SERVICE_AUTO_RESTART)
1161 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1163 if (state != SERVICE_START &&
1164 state != SERVICE_START_POST &&
1165 state != SERVICE_RUNNING &&
1166 state != SERVICE_RELOAD &&
1167 state != SERVICE_STOP &&
1168 state != SERVICE_STOP_SIGTERM &&
1169 state != SERVICE_STOP_SIGKILL)
1170 service_unwatch_main_pid(s);
1172 if (state != SERVICE_START_PRE &&
1173 state != SERVICE_START &&
1174 state != SERVICE_START_POST &&
1175 state != SERVICE_RELOAD &&
1176 state != SERVICE_STOP &&
1177 state != SERVICE_STOP_SIGTERM &&
1178 state != SERVICE_STOP_SIGKILL &&
1179 state != SERVICE_STOP_POST &&
1180 state != SERVICE_FINAL_SIGTERM &&
1181 state != SERVICE_FINAL_SIGKILL) {
1182 service_unwatch_control_pid(s);
1183 s->control_command = NULL;
1184 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1187 if (state == SERVICE_DEAD ||
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 state == SERVICE_FAILED ||
1195 state == SERVICE_AUTO_RESTART)
1196 service_notify_sockets_dead(s);
1198 if (state != SERVICE_START_PRE &&
1199 state != SERVICE_START &&
1200 state != SERVICE_START_POST &&
1201 state != SERVICE_RUNNING &&
1202 state != SERVICE_RELOAD &&
1203 state != SERVICE_STOP &&
1204 state != SERVICE_STOP_SIGTERM &&
1205 state != SERVICE_STOP_SIGKILL &&
1206 state != SERVICE_STOP_POST &&
1207 state != SERVICE_FINAL_SIGTERM &&
1208 state != SERVICE_FINAL_SIGKILL &&
1209 !(state == SERVICE_DEAD && s->meta.job)) {
1210 service_close_socket_fd(s);
1211 service_connection_unref(s);
1214 /* For the inactive states unit_notify() will trim the cgroup,
1215 * but for exit we have to do that ourselves... */
1216 if (state == SERVICE_EXITED)
1217 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1219 if (old_state != state)
1220 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1222 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1225 static int service_coldplug(Unit *u) {
1226 Service *s = SERVICE(u);
1230 assert(s->state == SERVICE_DEAD);
1232 if (s->deserialized_state != s->state) {
1234 if (s->deserialized_state == SERVICE_START_PRE ||
1235 s->deserialized_state == SERVICE_START ||
1236 s->deserialized_state == SERVICE_START_POST ||
1237 s->deserialized_state == SERVICE_RELOAD ||
1238 s->deserialized_state == SERVICE_STOP ||
1239 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1240 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1241 s->deserialized_state == SERVICE_STOP_POST ||
1242 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1243 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1244 s->deserialized_state == SERVICE_AUTO_RESTART) {
1246 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1249 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1251 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1256 if ((s->deserialized_state == SERVICE_START &&
1257 (s->type == SERVICE_FORKING ||
1258 s->type == SERVICE_DBUS ||
1259 s->type == SERVICE_ONESHOT ||
1260 s->type == SERVICE_NOTIFY)) ||
1261 s->deserialized_state == SERVICE_START_POST ||
1262 s->deserialized_state == SERVICE_RUNNING ||
1263 s->deserialized_state == SERVICE_RELOAD ||
1264 s->deserialized_state == SERVICE_STOP ||
1265 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1266 s->deserialized_state == SERVICE_STOP_SIGKILL)
1267 if (s->main_pid > 0)
1268 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1271 if (s->deserialized_state == SERVICE_START_PRE ||
1272 s->deserialized_state == SERVICE_START ||
1273 s->deserialized_state == SERVICE_START_POST ||
1274 s->deserialized_state == SERVICE_RELOAD ||
1275 s->deserialized_state == SERVICE_STOP ||
1276 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1277 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1278 s->deserialized_state == SERVICE_STOP_POST ||
1279 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1280 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1281 if (s->control_pid > 0)
1282 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1285 service_set_state(s, s->deserialized_state);
1291 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1295 unsigned rn_fds = 0;
1303 if (s->socket_fd >= 0)
1306 if ((r = service_get_sockets(s, &set)) < 0)
1309 SET_FOREACH(sock, set, i) {
1313 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1325 if (!(t = new(int, rn_fds+cn_fds))) {
1331 memcpy(t, rfds, rn_fds);
1332 memcpy(t+rn_fds, cfds, cn_fds);
1337 rn_fds = rn_fds+cn_fds;
1355 static int service_spawn(
1360 bool apply_permissions,
1362 bool apply_tty_stdin,
1363 bool set_notify_socket,
1368 int *fds = NULL, *fdsbuf = NULL;
1369 unsigned n_fds = 0, n_env = 0;
1370 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1377 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1378 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1379 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1381 if (s->socket_fd >= 0) {
1382 fds = &s->socket_fd;
1385 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1392 if (timeout && s->timeout_usec) {
1393 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1396 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1398 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1403 if (!(our_env = new0(char*, 3))) {
1408 if (set_notify_socket)
1409 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1414 if (s->main_pid > 0)
1415 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1420 if (!(final_env = strv_env_merge(2,
1421 s->meta.manager->environment,
1436 s->meta.manager->confirm_spawn,
1437 s->meta.cgroup_bondings,
1444 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1445 /* FIXME: we need to do something here */
1451 strv_free(final_env);
1461 strv_free(final_env);
1464 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1469 static int main_pid_good(Service *s) {
1472 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1475 /* If we know the pid file, then lets just check if it is
1477 if (s->main_pid_known)
1478 return s->main_pid > 0;
1480 /* We don't know the pid */
1484 static int control_pid_good(Service *s) {
1487 return s->control_pid > 0;
1490 static int cgroup_good(Service *s) {
1495 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1501 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1508 if (allow_restart &&
1509 !s->forbid_restart &&
1510 (s->restart == SERVICE_RESTART_ALWAYS ||
1511 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1513 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1516 service_set_state(s, SERVICE_AUTO_RESTART);
1518 service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD);
1520 s->forbid_restart = false;
1525 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1526 service_enter_dead(s, false, false);
1529 static void service_enter_signal(Service *s, ServiceState state, bool success);
1531 static void service_enter_stop_post(Service *s, bool success) {
1538 service_unwatch_control_pid(s);
1540 s->control_command_id = SERVICE_EXEC_STOP_POST;
1541 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1542 if ((r = service_spawn(s,
1546 !s->permissions_start_only,
1547 !s->root_directory_start_only,
1550 &s->control_pid)) < 0)
1554 service_set_state(s, SERVICE_STOP_POST);
1556 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1561 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1562 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1565 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1567 Set *pid_set = NULL;
1568 bool wait_for_exit = false;
1575 if (s->exec_context.kill_mode != KILL_NONE) {
1576 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1578 if (s->main_pid > 0) {
1579 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1581 s->main_pid, sig) < 0 && errno != ESRCH)
1583 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1585 wait_for_exit = true;
1588 if (s->control_pid > 0) {
1589 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1591 s->control_pid, sig) < 0 && errno != ESRCH)
1593 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1595 wait_for_exit = true;
1598 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1600 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1605 /* Exclude the main/control pids from being killed via the cgroup */
1606 if (s->main_pid > 0)
1607 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1610 if (s->control_pid > 0)
1611 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1614 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
1615 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1616 log_warning("Failed to kill control group: %s", strerror(-r));
1618 wait_for_exit = true;
1624 if (wait_for_exit) {
1625 if (s->timeout_usec > 0)
1626 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1629 service_set_state(s, state);
1630 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1631 service_enter_stop_post(s, true);
1633 service_enter_dead(s, true, true);
1638 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1640 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1641 service_enter_stop_post(s, false);
1643 service_enter_dead(s, false, true);
1649 static void service_enter_stop(Service *s, bool success) {
1657 service_unwatch_control_pid(s);
1659 s->control_command_id = SERVICE_EXEC_STOP;
1660 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1661 if ((r = service_spawn(s,
1665 !s->permissions_start_only,
1666 !s->root_directory_start_only,
1669 &s->control_pid)) < 0)
1672 service_set_state(s, SERVICE_STOP);
1674 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1679 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1680 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1683 static void service_enter_running(Service *s, bool success) {
1684 int main_pid_ok, cgroup_ok;
1690 main_pid_ok = main_pid_good(s);
1691 cgroup_ok = cgroup_good(s);
1693 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1694 (s->bus_name_good || s->type != SERVICE_DBUS))
1695 service_set_state(s, SERVICE_RUNNING);
1696 else if (s->remain_after_exit)
1697 service_set_state(s, SERVICE_EXITED);
1699 service_enter_stop(s, true);
1702 static void service_enter_start_post(Service *s) {
1706 service_unwatch_control_pid(s);
1708 s->control_command_id = SERVICE_EXEC_START_POST;
1709 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1710 if ((r = service_spawn(s,
1714 !s->permissions_start_only,
1715 !s->root_directory_start_only,
1718 &s->control_pid)) < 0)
1721 service_set_state(s, SERVICE_START_POST);
1723 service_enter_running(s, true);
1728 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1729 service_enter_stop(s, false);
1732 static void service_enter_start(Service *s) {
1738 assert(s->exec_command[SERVICE_EXEC_START]);
1739 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1741 if (s->type == SERVICE_FORKING)
1742 service_unwatch_control_pid(s);
1744 service_unwatch_main_pid(s);
1746 s->control_command_id = SERVICE_EXEC_START;
1747 s->control_command = s->exec_command[SERVICE_EXEC_START];
1749 if ((r = service_spawn(s,
1751 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1756 s->notify_access != NOTIFY_NONE,
1760 if (s->type == SERVICE_SIMPLE) {
1761 /* For simple services we immediately start
1762 * the START_POST binaries. */
1764 service_set_main_pid(s, pid);
1765 service_enter_start_post(s);
1767 } else if (s->type == SERVICE_FORKING) {
1769 /* For forking services we wait until the start
1770 * process exited. */
1772 s->control_pid = pid;
1773 service_set_state(s, SERVICE_START);
1775 } else if (s->type == SERVICE_ONESHOT ||
1776 s->type == SERVICE_DBUS ||
1777 s->type == SERVICE_NOTIFY) {
1779 /* For oneshot services we wait until the start
1780 * process exited, too, but it is our main process. */
1782 /* For D-Bus services we know the main pid right away,
1783 * but wait for the bus name to appear on the
1784 * bus. Notify services are similar. */
1786 service_set_main_pid(s, pid);
1787 service_set_state(s, SERVICE_START);
1789 assert_not_reached("Unknown service type");
1794 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1795 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1798 static void service_enter_start_pre(Service *s) {
1803 service_unwatch_control_pid(s);
1805 s->control_command_id = SERVICE_EXEC_START_PRE;
1806 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1807 if ((r = service_spawn(s,
1811 !s->permissions_start_only,
1812 !s->root_directory_start_only,
1815 &s->control_pid)) < 0)
1818 service_set_state(s, SERVICE_START_PRE);
1820 service_enter_start(s);
1825 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1826 service_enter_dead(s, false, true);
1829 static void service_enter_restart(Service *s) {
1834 dbus_error_init(&error);
1836 service_enter_dead(s, true, false);
1838 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
1841 log_debug("%s scheduled restart job.", s->meta.id);
1845 log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
1846 service_enter_dead(s, false, false);
1848 dbus_error_free(&error);
1851 static void service_enter_reload(Service *s) {
1856 service_unwatch_control_pid(s);
1858 s->control_command_id = SERVICE_EXEC_RELOAD;
1859 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1860 if ((r = service_spawn(s,
1864 !s->permissions_start_only,
1865 !s->root_directory_start_only,
1868 &s->control_pid)) < 0)
1871 service_set_state(s, SERVICE_RELOAD);
1873 service_enter_running(s, true);
1878 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1879 service_enter_stop(s, false);
1882 static void service_run_next_control(Service *s, bool success) {
1886 assert(s->control_command);
1887 assert(s->control_command->command_next);
1892 assert(s->control_command_id != SERVICE_EXEC_START);
1894 s->control_command = s->control_command->command_next;
1895 service_unwatch_control_pid(s);
1897 if ((r = service_spawn(s,
1901 !s->permissions_start_only,
1902 !s->root_directory_start_only,
1903 s->control_command_id == SERVICE_EXEC_START_PRE ||
1904 s->control_command_id == SERVICE_EXEC_STOP_POST,
1906 &s->control_pid)) < 0)
1912 log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
1914 if (s->state == SERVICE_START_PRE)
1915 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1916 else if (s->state == SERVICE_STOP)
1917 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1918 else if (s->state == SERVICE_STOP_POST)
1919 service_enter_dead(s, false, true);
1921 service_enter_stop(s, false);
1924 static void service_run_next_main(Service *s, bool success) {
1929 assert(s->control_command);
1930 assert(s->control_command->command_next);
1935 assert(s->control_command_id == SERVICE_EXEC_START);
1936 assert(s->type == SERVICE_ONESHOT);
1938 s->control_command = s->control_command->command_next;
1939 service_unwatch_main_pid(s);
1941 if ((r = service_spawn(s,
1948 s->notify_access != NOTIFY_NONE,
1952 service_set_main_pid(s, pid);
1957 log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
1958 service_enter_stop(s, false);
1961 static int service_start(Unit *u) {
1962 Service *s = SERVICE(u);
1966 /* We cannot fulfill this request right now, try again later
1968 if (s->state == SERVICE_STOP ||
1969 s->state == SERVICE_STOP_SIGTERM ||
1970 s->state == SERVICE_STOP_SIGKILL ||
1971 s->state == SERVICE_STOP_POST ||
1972 s->state == SERVICE_FINAL_SIGTERM ||
1973 s->state == SERVICE_FINAL_SIGKILL)
1976 /* Already on it! */
1977 if (s->state == SERVICE_START_PRE ||
1978 s->state == SERVICE_START ||
1979 s->state == SERVICE_START_POST)
1982 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
1984 /* Make sure we don't enter a busy loop of some kind. */
1985 if (!ratelimit_test(&s->ratelimit)) {
1986 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1990 if ((s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1991 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1992 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) &&
1994 log_warning("%s can only be started with a per-connection socket.", u->meta.id);
1999 s->main_pid_known = false;
2000 s->forbid_restart = false;
2002 service_enter_start_pre(s);
2006 static int service_stop(Unit *u) {
2007 Service *s = SERVICE(u);
2011 /* This is a user request, so don't do restarts on this
2013 s->forbid_restart = true;
2016 if (s->state == SERVICE_STOP ||
2017 s->state == SERVICE_STOP_SIGTERM ||
2018 s->state == SERVICE_STOP_SIGKILL ||
2019 s->state == SERVICE_STOP_POST ||
2020 s->state == SERVICE_FINAL_SIGTERM ||
2021 s->state == SERVICE_FINAL_SIGKILL)
2024 /* Don't allow a restart */
2025 if (s->state == SERVICE_AUTO_RESTART) {
2026 service_set_state(s, SERVICE_DEAD);
2030 /* If there's already something running we go directly into
2032 if (s->state == SERVICE_START_PRE ||
2033 s->state == SERVICE_START ||
2034 s->state == SERVICE_START_POST ||
2035 s->state == SERVICE_RELOAD) {
2036 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2040 assert(s->state == SERVICE_RUNNING ||
2041 s->state == SERVICE_EXITED);
2043 service_enter_stop(s, true);
2047 static int service_reload(Unit *u) {
2048 Service *s = SERVICE(u);
2052 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2054 service_enter_reload(s);
2058 static bool service_can_reload(Unit *u) {
2059 Service *s = SERVICE(u);
2063 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2066 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2067 Service *s = SERVICE(u);
2073 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2074 unit_serialize_item(u, f, "failure", yes_no(s->failure));
2076 if (s->control_pid > 0)
2077 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2079 if (s->main_pid_known && s->main_pid > 0)
2080 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2082 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2085 unit_serialize_item(u, f, "status-text", s->status_text);
2087 /* There's a minor uncleanliness here: if there are multiple
2088 * commands attached here, we will start from the first one
2090 if (s->control_command_id >= 0)
2091 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2093 if (s->socket_fd >= 0) {
2096 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2099 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2102 if (s->main_exec_status.pid > 0) {
2103 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2105 if (s->main_exec_status.start_timestamp.realtime > 0) {
2106 unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2107 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2109 unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2110 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2113 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2114 unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2115 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2116 unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2117 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2119 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2120 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2127 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2128 Service *s = SERVICE(u);
2135 if (streq(key, "state")) {
2138 if ((state = service_state_from_string(value)) < 0)
2139 log_debug("Failed to parse state value %s", value);
2141 s->deserialized_state = state;
2142 } else if (streq(key, "failure")) {
2145 if ((b = parse_boolean(value)) < 0)
2146 log_debug("Failed to parse failure value %s", value);
2148 s->failure = b || s->failure;
2149 } else if (streq(key, "control-pid")) {
2152 if (parse_pid(value, &pid) < 0)
2153 log_debug("Failed to parse control-pid value %s", value);
2155 s->control_pid = pid;
2156 } else if (streq(key, "main-pid")) {
2159 if (parse_pid(value, &pid) < 0)
2160 log_debug("Failed to parse main-pid value %s", value);
2162 service_set_main_pid(s, (pid_t) pid);
2163 } else if (streq(key, "main-pid-known")) {
2166 if ((b = parse_boolean(value)) < 0)
2167 log_debug("Failed to parse main-pid-known value %s", value);
2169 s->main_pid_known = b;
2170 } else if (streq(key, "status-text")) {
2173 if ((t = strdup(value))) {
2174 free(s->status_text);
2178 } else if (streq(key, "control-command")) {
2179 ServiceExecCommand id;
2181 if ((id = service_exec_command_from_string(value)) < 0)
2182 log_debug("Failed to parse exec-command value %s", value);
2184 s->control_command_id = id;
2185 s->control_command = s->exec_command[id];
2187 } else if (streq(key, "socket-fd")) {
2190 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2191 log_debug("Failed to parse socket-fd value %s", value);
2194 if (s->socket_fd >= 0)
2195 close_nointr_nofail(s->socket_fd);
2196 s->socket_fd = fdset_remove(fds, fd);
2198 } else if (streq(key, "main-exec-status-pid")) {
2201 if (parse_pid(value, &pid) < 0)
2202 log_debug("Failed to parse main-exec-status-pid value %s", value);
2204 s->main_exec_status.pid = pid;
2205 } else if (streq(key, "main-exec-status-code")) {
2208 if (safe_atoi(value, &i) < 0)
2209 log_debug("Failed to parse main-exec-status-code value %s", value);
2211 s->main_exec_status.code = i;
2212 } else if (streq(key, "main-exec-status-status")) {
2215 if (safe_atoi(value, &i) < 0)
2216 log_debug("Failed to parse main-exec-status-status value %s", value);
2218 s->main_exec_status.status = i;
2219 } else if (streq(key, "main-exec-status-start-realtime")) {
2222 if (safe_atou64(value, &k) < 0)
2223 log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2225 s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2226 } else if (streq(key, "main-exec-status-start-monotonic")) {
2229 if (safe_atou64(value, &k) < 0)
2230 log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2232 s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2233 } else if (streq(key, "main-exec-status-exit-realtime")) {
2236 if (safe_atou64(value, &k) < 0)
2237 log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2239 s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2240 } else if (streq(key, "main-exec-status-exit-monotonic")) {
2243 if (safe_atou64(value, &k) < 0)
2244 log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2246 s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2248 log_debug("Unknown serialization key '%s'", key);
2253 static UnitActiveState service_active_state(Unit *u) {
2256 return state_translation_table[SERVICE(u)->state];
2259 static const char *service_sub_state_to_string(Unit *u) {
2262 return service_state_to_string(SERVICE(u)->state);
2265 static bool service_check_gc(Unit *u) {
2266 Service *s = SERVICE(u);
2270 return !!s->sysv_path;
2273 static bool service_check_snapshot(Unit *u) {
2274 Service *s = SERVICE(u);
2278 return !s->got_socket_fd;
2281 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2282 Service *s = SERVICE(u);
2288 if (!s->meta.fragment_path)
2289 success = is_clean_exit_lsb(code, status);
2291 success = is_clean_exit(code, status);
2293 if (s->main_pid == pid) {
2296 exec_status_exit(&s->main_exec_status, pid, code, status);
2298 if (s->type != SERVICE_FORKING && s->control_command) {
2299 s->control_command->exec_status = s->main_exec_status;
2301 if (s->control_command->ignore)
2305 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2306 "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2307 s->failure = s->failure || !success;
2309 if (s->control_command &&
2310 s->control_command->command_next &&
2313 /* There is another command to *
2314 * execute, so let's do that. */
2316 log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2317 service_run_next_main(s, success);
2321 /* The service exited, so the service is officially
2324 s->control_command = NULL;
2325 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2329 case SERVICE_START_POST:
2330 case SERVICE_RELOAD:
2332 /* Need to wait until the operation is
2337 if (s->type == SERVICE_ONESHOT) {
2338 /* This was our main goal, so let's go on */
2340 service_enter_start_post(s);
2342 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2345 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2350 case SERVICE_RUNNING:
2351 service_enter_running(s, success);
2354 case SERVICE_STOP_SIGTERM:
2355 case SERVICE_STOP_SIGKILL:
2357 if (!control_pid_good(s))
2358 service_enter_stop_post(s, success);
2360 /* If there is still a control process, wait for that first */
2364 assert_not_reached("Uh, main process died at wrong time.");
2368 } else if (s->control_pid == pid) {
2372 if (s->control_command) {
2373 exec_status_exit(&s->control_command->exec_status, pid, code, status);
2375 if (s->control_command->ignore)
2379 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2380 "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2381 s->failure = s->failure || !success;
2383 if (s->control_command &&
2384 s->control_command->command_next &&
2387 /* There is another command to *
2388 * execute, so let's do that. */
2390 log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2391 service_run_next_control(s, success);
2394 /* No further commands for this step, so let's
2395 * figure out what to do next */
2397 s->control_command = NULL;
2398 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2400 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2404 case SERVICE_START_PRE:
2406 service_enter_start(s);
2408 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2412 assert(s->type == SERVICE_FORKING);
2414 /* Let's try to load the pid
2415 * file here if we can. We
2416 * ignore the return value,
2417 * since the PID file might
2418 * actually be created by a
2419 * START_POST script */
2423 service_load_pid_file(s);
2425 service_enter_start_post(s);
2427 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2431 case SERVICE_START_POST:
2432 if (success && s->pid_file && !s->main_pid_known) {
2435 /* Hmm, let's see if we can
2436 * load the pid now after the
2437 * start-post scripts got
2440 if ((r = service_load_pid_file(s)) < 0)
2441 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2446 case SERVICE_RELOAD:
2448 service_enter_running(s, true);
2450 service_enter_stop(s, false);
2455 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2458 case SERVICE_STOP_SIGTERM:
2459 case SERVICE_STOP_SIGKILL:
2460 if (main_pid_good(s) <= 0)
2461 service_enter_stop_post(s, success);
2463 /* If there is still a service
2464 * process around, wait until
2465 * that one quit, too */
2468 case SERVICE_STOP_POST:
2469 case SERVICE_FINAL_SIGTERM:
2470 case SERVICE_FINAL_SIGKILL:
2471 service_enter_dead(s, success, true);
2475 assert_not_reached("Uh, control process died at wrong time.");
2480 /* Notify clients about changed exit status */
2481 unit_add_to_dbus_queue(u);
2484 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2485 Service *s = SERVICE(u);
2488 assert(elapsed == 1);
2490 assert(w == &s->timer_watch);
2494 case SERVICE_START_PRE:
2496 log_warning("%s operation timed out. Terminating.", u->meta.id);
2497 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2500 case SERVICE_START_POST:
2501 case SERVICE_RELOAD:
2502 log_warning("%s operation timed out. Stopping.", u->meta.id);
2503 service_enter_stop(s, false);
2507 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2508 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2511 case SERVICE_STOP_SIGTERM:
2512 log_warning("%s stopping timed out. Killing.", u->meta.id);
2513 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2516 case SERVICE_STOP_SIGKILL:
2517 /* Uh, wie sent a SIGKILL and it is still not gone?
2518 * Must be something we cannot kill, so let's just be
2519 * weirded out and continue */
2521 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2522 service_enter_stop_post(s, false);
2525 case SERVICE_STOP_POST:
2526 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2527 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2530 case SERVICE_FINAL_SIGTERM:
2531 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2532 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2535 case SERVICE_FINAL_SIGKILL:
2536 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
2537 service_enter_dead(s, false, true);
2540 case SERVICE_AUTO_RESTART:
2541 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2542 service_enter_restart(s);
2546 assert_not_reached("Timeout at wrong time.");
2550 static void service_cgroup_notify_event(Unit *u) {
2551 Service *s = SERVICE(u);
2555 log_debug("%s: cgroup is empty", u->meta.id);
2559 /* Waiting for SIGCHLD is usually more interesting,
2560 * because it includes return codes/signals. Which is
2561 * why we ignore the cgroup events for most cases,
2562 * except when we don't know pid which to expect the
2565 case SERVICE_RUNNING:
2566 service_enter_running(s, true);
2569 case SERVICE_STOP_SIGTERM:
2570 case SERVICE_STOP_SIGKILL:
2571 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2572 service_enter_stop_post(s, true);
2576 case SERVICE_FINAL_SIGTERM:
2577 case SERVICE_FINAL_SIGKILL:
2578 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2579 service_enter_dead(s, true, true);
2588 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2589 Service *s = SERVICE(u);
2594 if (s->notify_access == NOTIFY_NONE) {
2595 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2596 u->meta.id, (unsigned long) pid);
2600 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2601 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2602 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2606 log_debug("%s: Got message", u->meta.id);
2608 /* Interpret MAINPID= */
2609 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2610 (s->state == SERVICE_START ||
2611 s->state == SERVICE_START_POST ||
2612 s->state == SERVICE_RUNNING ||
2613 s->state == SERVICE_RELOAD)) {
2615 if (parse_pid(e + 8, &pid) < 0)
2616 log_warning("Failed to parse notification message %s", e);
2618 log_debug("%s: got %s", u->meta.id, e);
2619 service_set_main_pid(s, pid);
2623 /* Interpret READY= */
2624 if (s->type == SERVICE_NOTIFY &&
2625 s->state == SERVICE_START &&
2626 strv_find(tags, "READY=1")) {
2627 log_debug("%s: got READY=1", u->meta.id);
2629 service_enter_start_post(s);
2632 /* Interpret STATUS= */
2633 if ((e = strv_find_prefix(tags, "STATUS="))) {
2637 if (!(t = strdup(e+7))) {
2638 log_error("Failed to allocate string.");
2642 log_debug("%s: got %s", u->meta.id, e);
2644 free(s->status_text);
2647 free(s->status_text);
2648 s->status_text = NULL;
2653 /* Notify clients about changed status or main pid */
2654 unit_add_to_dbus_queue(u);
2657 static int service_enumerate(Manager *m) {
2661 char *path = NULL, *fpath = NULL, *name = NULL;
2662 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
2667 Unit *previous = NULL;
2668 char *arch_daemons = NULL;
2669 char *arch_daemons_stripped = NULL;
2670 char **arch_daemons_split = NULL;
2676 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
2677 "DAEMONS", &arch_daemons,
2681 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
2683 } else if (arch_daemons) {
2684 if (!(arch_daemons_stripped = strchr(arch_daemons, '(')))
2685 arch_daemons_stripped = arch_daemons;
2687 arch_daemons_stripped++; /* strip start paren */
2689 arch_daemons_stripped[strcspn(arch_daemons_stripped, ")")] = 0; /* strip end paren */
2691 if (!(arch_daemons_split = strv_split_quoted(arch_daemons_stripped))) {
2696 STRV_FOREACH(p, arch_daemons_split) {
2700 if (**p == '!') /* daemons prefixed with ! are disabled, so ignore them */
2703 if (!(name = sysv_translate_name(*p))) {
2708 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2709 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2713 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, "multi-user.target", NULL, true)) < 0)
2717 if ((r = unit_add_dependency(service, UNIT_AFTER, previous, true)) < 0)
2720 if (**p != '@') /* daemons prefixed with @ can be started in the background */
2726 zero(runlevel_services);
2728 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2729 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2734 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2742 if (!(d = opendir(path))) {
2743 if (errno != ENOENT)
2744 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2749 while ((de = readdir(d))) {
2752 if (ignore_file(de->d_name))
2755 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2758 if (strlen(de->d_name) < 4)
2761 a = undecchar(de->d_name[1]);
2762 b = undecchar(de->d_name[2]);
2769 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2774 if (access(fpath, X_OK) < 0) {
2776 if (errno != ENOENT)
2777 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2783 if (!(name = sysv_translate_name(de->d_name + 3))) {
2788 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2789 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2793 if (de->d_name[0] == 'S') {
2795 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
2796 SERVICE(service)->sysv_start_priority =
2797 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2799 SERVICE(service)->sysv_enabled = true;
2802 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
2805 if ((r = set_put(runlevel_services[i], service)) < 0)
2808 } else if (de->d_name[0] == 'K' &&
2809 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2810 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2812 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
2815 if ((r = set_put(shutdown_services, service)) < 0)
2821 /* Now we loaded all stubs and are aware of the lowest
2822 start-up priority for all services, not let's actually load
2823 the services, this will also tell us which services are
2824 actually native now */
2825 manager_dispatch_load_queue(m);
2827 /* If this is a native service, rely on native ways to pull in
2828 * a service, don't pull it in via sysv rcN.d links. */
2829 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
2830 SET_FOREACH(service, runlevel_services[i], j) {
2831 service = unit_follow_merge(service);
2833 if (service->meta.fragment_path)
2836 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2840 /* We honour K links only for halt/reboot. For the normal
2841 * runlevels we assume the stop jobs will be implicitly added
2842 * by the core logic. Also, we don't really distuingish here
2843 * between the runlevels 0 and 6 and just add them to the
2844 * special shutdown target. On SUSE the boot.d/ runlevel is
2845 * also used for shutdown, so we add links for that too to the
2846 * shutdown target.*/
2847 SET_FOREACH(service, shutdown_services, j) {
2848 service = unit_follow_merge(service);
2850 if (service->meta.fragment_path)
2853 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2865 free(arch_daemons_split);
2868 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
2869 set_free(runlevel_services[i]);
2870 set_free(shutdown_services);
2878 static void service_bus_name_owner_change(
2881 const char *old_owner,
2882 const char *new_owner) {
2884 Service *s = SERVICE(u);
2889 assert(streq(s->bus_name, name));
2890 assert(old_owner || new_owner);
2892 if (old_owner && new_owner)
2893 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2895 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2897 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2899 s->bus_name_good = !!new_owner;
2901 if (s->type == SERVICE_DBUS) {
2903 /* service_enter_running() will figure out what to
2905 if (s->state == SERVICE_RUNNING)
2906 service_enter_running(s, true);
2907 else if (s->state == SERVICE_START && new_owner)
2908 service_enter_start_post(s);
2910 } else if (new_owner &&
2912 (s->state == SERVICE_START ||
2913 s->state == SERVICE_START_POST ||
2914 s->state == SERVICE_RUNNING ||
2915 s->state == SERVICE_RELOAD)) {
2917 /* Try to acquire PID from bus service */
2918 log_debug("Trying to acquire PID from D-Bus name...");
2920 bus_query_pid(u->meta.manager, name);
2924 static void service_bus_query_pid_done(
2929 Service *s = SERVICE(u);
2934 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2936 if (s->main_pid <= 0 &&
2937 (s->state == SERVICE_START ||
2938 s->state == SERVICE_START_POST ||
2939 s->state == SERVICE_RUNNING ||
2940 s->state == SERVICE_RELOAD))
2941 service_set_main_pid(s, pid);
2944 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2948 /* This is called by the socket code when instantiating a new
2949 * service for a stream socket and the socket needs to be
2952 if (s->meta.load_state != UNIT_LOADED)
2955 if (s->socket_fd >= 0)
2958 if (s->state != SERVICE_DEAD)
2962 s->got_socket_fd = true;
2968 static void service_reset_failed(Unit *u) {
2969 Service *s = SERVICE(u);
2973 if (s->state == SERVICE_FAILED)
2974 service_set_state(s, SERVICE_DEAD);
2979 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2980 [SERVICE_DEAD] = "dead",
2981 [SERVICE_START_PRE] = "start-pre",
2982 [SERVICE_START] = "start",
2983 [SERVICE_START_POST] = "start-post",
2984 [SERVICE_RUNNING] = "running",
2985 [SERVICE_EXITED] = "exited",
2986 [SERVICE_RELOAD] = "reload",
2987 [SERVICE_STOP] = "stop",
2988 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2989 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2990 [SERVICE_STOP_POST] = "stop-post",
2991 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2992 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2993 [SERVICE_FAILED] = "failed",
2994 [SERVICE_AUTO_RESTART] = "auto-restart",
2997 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2999 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3000 [SERVICE_ONCE] = "once",
3001 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
3002 [SERVICE_RESTART_ALWAYS] = "restart-always",
3005 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3007 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3008 [SERVICE_SIMPLE] = "simple",
3009 [SERVICE_FORKING] = "forking",
3010 [SERVICE_ONESHOT] = "oneshot",
3011 [SERVICE_DBUS] = "dbus",
3012 [SERVICE_NOTIFY] = "notify"
3015 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3017 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3018 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3019 [SERVICE_EXEC_START] = "ExecStart",
3020 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3021 [SERVICE_EXEC_RELOAD] = "ExecReload",
3022 [SERVICE_EXEC_STOP] = "ExecStop",
3023 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3026 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3028 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3029 [NOTIFY_NONE] = "none",
3030 [NOTIFY_MAIN] = "main",
3031 [NOTIFY_ALL] = "all"
3034 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3036 const UnitVTable service_vtable = {
3037 .suffix = ".service",
3038 .show_status = true,
3040 .init = service_init,
3041 .done = service_done,
3042 .load = service_load,
3044 .coldplug = service_coldplug,
3046 .dump = service_dump,
3048 .start = service_start,
3049 .stop = service_stop,
3050 .reload = service_reload,
3052 .can_reload = service_can_reload,
3054 .serialize = service_serialize,
3055 .deserialize_item = service_deserialize_item,
3057 .active_state = service_active_state,
3058 .sub_state_to_string = service_sub_state_to_string,
3060 .check_gc = service_check_gc,
3061 .check_snapshot = service_check_snapshot,
3063 .sigchld_event = service_sigchld_event,
3064 .timer_event = service_timer_event,
3066 .reset_failed = service_reset_failed,
3068 .cgroup_notify_empty = service_cgroup_notify_event,
3069 .notify_message = service_notify_message,
3071 .bus_name_owner_change = service_bus_name_owner_change,
3072 .bus_query_pid_done = service_bus_query_pid_done,
3074 .bus_interface = "org.freedesktop.systemd1.Service",
3075 .bus_message_handler = bus_service_message_handler,
3076 .bus_invalidating_properties = bus_service_invalidating_properties,
3078 .enumerate = service_enumerate