1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
36 #include "bus-errors.h"
38 #define COMMENTS "#;\n"
39 #define NEWLINES "\n\r"
41 #ifdef HAVE_SYSV_COMPAT
42 typedef enum RunlevelType {
51 const RunlevelType type;
53 /* Standard SysV runlevels for start-up */
54 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
55 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
56 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
57 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
58 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
61 /* SUSE style boot.d */
62 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
66 /* Debian style rcS.d */
67 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
70 /* Standard SysV runlevels for shutdown */
71 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
72 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
74 /* Note that the order here matters, as we read the
75 directories in this order, and we want to make sure that
76 sysv_start_priority is known when we first load the
77 unit. And that value we only know from S links. Hence
78 UP/SYSINIT must be read before DOWN */
81 #define RUNLEVELS_UP "12345"
82 /* #define RUNLEVELS_DOWN "06" */
83 /* #define RUNLEVELS_BOOT "bBsS" */
86 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
87 [SERVICE_DEAD] = UNIT_INACTIVE,
88 [SERVICE_START_PRE] = UNIT_ACTIVATING,
89 [SERVICE_START] = UNIT_ACTIVATING,
90 [SERVICE_START_POST] = UNIT_ACTIVATING,
91 [SERVICE_RUNNING] = UNIT_ACTIVE,
92 [SERVICE_EXITED] = UNIT_ACTIVE,
93 [SERVICE_RELOAD] = UNIT_RELOADING,
94 [SERVICE_STOP] = UNIT_DEACTIVATING,
95 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
96 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
97 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
98 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
99 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
100 [SERVICE_FAILED] = UNIT_FAILED,
101 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
104 static void service_init(Unit *u) {
105 Service *s = SERVICE(u);
108 assert(u->meta.load_state == UNIT_STUB);
110 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
111 s->restart_usec = DEFAULT_RESTART_USEC;
112 s->timer_watch.type = WATCH_INVALID;
113 #ifdef HAVE_SYSV_COMPAT
114 s->sysv_start_priority = -1;
118 exec_context_init(&s->exec_context);
120 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
122 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
125 static void service_unwatch_control_pid(Service *s) {
128 if (s->control_pid <= 0)
131 unit_unwatch_pid(UNIT(s), s->control_pid);
135 static void service_unwatch_main_pid(Service *s) {
138 if (s->main_pid <= 0)
141 unit_unwatch_pid(UNIT(s), s->main_pid);
145 static int service_set_main_pid(Service *s, pid_t pid) {
156 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
157 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
158 s->meta.id, (unsigned long) pid);
161 s->main_pid_known = true;
163 exec_status_start(&s->main_exec_status, pid);
168 static void service_close_socket_fd(Service *s) {
171 if (s->socket_fd < 0)
174 close_nointr_nofail(s->socket_fd);
178 static void service_connection_unref(Service *s) {
184 socket_connection_unref(s->socket);
188 static void service_done(Unit *u) {
189 Service *s = SERVICE(u);
196 #ifdef HAVE_SYSV_COMPAT
200 free(s->sysv_runlevels);
201 s->sysv_runlevels = NULL;
204 free(s->status_text);
205 s->status_text = NULL;
207 exec_context_done(&s->exec_context);
208 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
209 s->control_command = NULL;
211 /* This will leak a process, but at least no memory or any of
213 service_unwatch_main_pid(s);
214 service_unwatch_control_pid(s);
217 unit_unwatch_bus_name(UNIT(u), s->bus_name);
222 service_close_socket_fd(s);
223 service_connection_unref(s);
225 unit_unwatch_timer(u, &s->timer_watch);
228 #ifdef HAVE_SYSV_COMPAT
229 static char *sysv_translate_name(const char *name) {
232 if (!(r = new(char, strlen(name) + sizeof(".service"))))
235 if (startswith(name, "boot."))
236 /* Drop SuSE-style boot. prefix */
237 strcpy(stpcpy(r, name + 5), ".service");
238 else if (endswith(name, ".sh"))
239 /* Drop Debian-style .sh suffix */
240 strcpy(stpcpy(r, name) - 3, ".service");
242 else if (startswith(name, "@"))
243 /* Drop Arch-style background prefix */
244 strcpy(stpcpy(r, name + 1), ".service");
247 /* Normal init scripts */
248 strcpy(stpcpy(r, name), ".service");
253 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
255 /* We silently ignore the $ prefix here. According to the LSB
256 * spec it simply indicates whether something is a
257 * standardized name or a distribution-specific one. Since we
258 * just follow what already exists and do not introduce new
259 * uses or names we don't care who introduced a new name. */
261 static const char * const table[] = {
262 /* LSB defined facilities */
263 "local_fs", SPECIAL_LOCAL_FS_TARGET,
264 "network", SPECIAL_NETWORK_TARGET,
265 "named", SPECIAL_NSS_LOOKUP_TARGET,
266 "portmap", SPECIAL_RPCBIND_TARGET,
267 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
268 "syslog", SPECIAL_SYSLOG_TARGET,
269 "time", SPECIAL_RTC_SET_TARGET,
271 /* Debian extensions */
273 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
275 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
276 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
279 /* Fedora extensions */
280 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
281 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
282 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
285 /* SuSE extensions */
297 n = *name == '$' ? name + 1 : name;
299 for (i = 0; i < ELEMENTSOF(table); i += 2) {
301 if (!streq(table[i], n))
307 if (!(r = strdup(table[i+1])))
313 /* If we don't know this name, fallback heuristics to figure
314 * out whether something is a target or an service alias. */
317 /* Facilities starting with $ are most likely targets */
318 r = unit_name_build(n, NULL, ".target");
319 else if (filename && streq(name, filename))
320 /* Names equalling the file name of the services are redundant */
323 /* Everything else we assume to be normal service names */
324 r = sysv_translate_name(n);
337 static int sysv_fix_order(Service *s) {
343 if (s->sysv_start_priority < 0)
346 /* For each pair of services where at least one lacks a LSB
347 * header, we use the start priority value to order things. */
349 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
352 bool special_s, special_t;
354 t = (Service*) other;
359 if (t->meta.load_state != UNIT_LOADED)
362 if (t->sysv_start_priority < 0)
365 /* If both units have modern headers we don't care
366 * about the priorities */
367 if ((s->meta.fragment_path || s->sysv_has_lsb) &&
368 (t->meta.fragment_path || t->sysv_has_lsb))
371 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
372 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
374 if (special_t && !special_s)
376 else if (special_s && !special_t)
378 else if (t->sysv_start_priority < s->sysv_start_priority)
380 else if (t->sysv_start_priority > s->sysv_start_priority)
385 /* FIXME: Maybe we should compare the name here lexicographically? */
387 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
394 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
397 if (!(c = new0(ExecCommand, 1)))
400 if (!(c->path = strdup(path))) {
405 if (!(c->argv = strv_new(path, arg1, NULL))) {
414 static int sysv_exec_commands(Service *s) {
418 assert(s->sysv_path);
420 if (!(c = exec_command_new(s->sysv_path, "start")))
422 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
424 if (!(c = exec_command_new(s->sysv_path, "stop")))
426 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
428 if (!(c = exec_command_new(s->sysv_path, "reload")))
430 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
435 static int service_load_sysv_path(Service *s, const char *path) {
446 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
453 if (!(f = fopen(path, "re"))) {
454 r = errno == ENOENT ? 0 : -errno;
459 if (!(s->sysv_path = strdup(path))) {
465 char l[LINE_MAX], *t;
467 if (!fgets(l, sizeof(l), f)) {
472 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
482 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
484 s->sysv_has_lsb = true;
488 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
494 t += strspn(t, WHITESPACE);
496 if (state == NORMAL) {
498 /* Try to parse Red Hat style chkconfig headers */
500 if (startswith_no_case(t, "chkconfig:")) {
502 char runlevels[16], *k;
506 if (sscanf(t+10, "%15s %i %*i",
508 &start_priority) != 2) {
510 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
514 /* A start priority gathered from the
515 * symlink farms is preferred over the
516 * data from the LSB header. */
517 if (start_priority < 0 || start_priority > 99)
518 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
519 else if (s->sysv_start_priority < 0)
520 s->sysv_start_priority = start_priority;
522 char_array_0(runlevels);
523 k = delete_chars(runlevels, WHITESPACE "-");
528 if (!(d = strdup(k))) {
533 free(s->sysv_runlevels);
534 s->sysv_runlevels = d;
537 } else if (startswith_no_case(t, "description:")) {
539 size_t k = strlen(t);
543 if (t[k-1] == '\\') {
548 if ((j = strstrip(t+12)) && *j) {
549 if (!(d = strdup(j))) {
556 free(chkconfig_description);
557 chkconfig_description = d;
559 } else if (startswith_no_case(t, "pidfile:")) {
566 if (!path_is_absolute(fn)) {
567 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
571 if (!(fn = strdup(fn))) {
580 } else if (state == DESCRIPTION) {
582 /* Try to parse Red Hat style description
585 size_t k = strlen(t);
593 if ((j = strstrip(t)) && *j) {
596 if (chkconfig_description)
597 asprintf(&d, "%s %s", chkconfig_description, j);
606 free(chkconfig_description);
607 chkconfig_description = d;
610 } else if (state == LSB || state == LSB_DESCRIPTION) {
612 if (startswith_no_case(t, "Provides:")) {
618 FOREACH_WORD_QUOTED(w, z, t+9, i) {
621 if (!(n = strndup(w, z))) {
626 r = sysv_translate_facility(n, file_name_from_path(path), &m);
635 if (unit_name_to_type(m) == UNIT_SERVICE)
636 r = unit_add_name(u, m);
638 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
640 if (s->sysv_enabled) {
643 if ((k = unit_add_dependency_by_name_inverse(u, UNIT_WANTS, m, NULL, true)) < 0)
649 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
654 } else if (startswith_no_case(t, "Required-Start:") ||
655 startswith_no_case(t, "Should-Start:") ||
656 startswith_no_case(t, "X-Start-Before:") ||
657 startswith_no_case(t, "X-Start-After:")) {
663 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
666 if (!(n = strndup(w, z))) {
671 r = sysv_translate_facility(n, file_name_from_path(path), &m);
680 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
683 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
687 } else if (startswith_no_case(t, "Default-Start:")) {
692 k = delete_chars(t+14, WHITESPACE "-");
695 if (!(d = strdup(k))) {
700 free(s->sysv_runlevels);
701 s->sysv_runlevels = d;
704 } else if (startswith_no_case(t, "Description:")) {
707 state = LSB_DESCRIPTION;
709 if ((j = strstrip(t+12)) && *j) {
710 if (!(d = strdup(j))) {
717 free(long_description);
718 long_description = d;
720 } else if (startswith_no_case(t, "Short-Description:")) {
725 if ((j = strstrip(t+18)) && *j) {
726 if (!(d = strdup(j))) {
733 free(short_description);
734 short_description = d;
736 } else if (startswith_no_case(t, "X-Interactive:")) {
739 if ((b = parse_boolean(strstrip(t+14))) < 0) {
740 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
745 s->exec_context.std_input = EXEC_INPUT_TTY;
747 s->exec_context.std_input = EXEC_INPUT_NULL;
749 } else if (state == LSB_DESCRIPTION) {
751 if (startswith(l, "#\t") || startswith(l, "# ")) {
754 if ((j = strstrip(t)) && *j) {
757 if (long_description)
758 asprintf(&d, "%s %s", long_description, t);
767 free(long_description);
768 long_description = d;
777 if ((r = sysv_exec_commands(s)) < 0)
780 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
781 /* If there a runlevels configured for this service
782 * but none of the standard ones, then we assume this
783 * is some special kind of service (which might be
784 * needed for early boot) and don't create any links
787 s->meta.default_dependencies = false;
789 /* Don't timeout special services during boot (like fsck) */
793 /* Special setting for all SysV services */
794 s->type = SERVICE_FORKING;
795 s->remain_after_exit = true;
796 s->restart = SERVICE_ONCE;
797 s->exec_context.std_output =
798 (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY)
799 ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
800 s->exec_context.kill_mode = KILL_PROCESS_GROUP;
802 /* We use the long description only if
803 * no short description is set. */
805 if (short_description)
806 description = short_description;
807 else if (chkconfig_description)
808 description = chkconfig_description;
809 else if (long_description)
810 description = long_description;
817 if (!(d = strappend("LSB: ", description))) {
822 u->meta.description = d;
825 u->meta.load_state = UNIT_LOADED;
833 free(short_description);
834 free(long_description);
835 free(chkconfig_description);
840 static int service_load_sysv_name(Service *s, const char *name) {
846 /* For SysV services we strip the boot. or .sh
847 * prefixes/suffixes. */
848 if (startswith(name, "boot.") ||
849 endswith(name, ".sh.service"))
852 STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
856 if (asprintf(&path, "%s/%s", *p, name) < 0)
859 assert(endswith(path, ".service"));
860 path[strlen(path)-8] = 0;
862 r = service_load_sysv_path(s, path);
864 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
865 /* Try Debian style xxx.sh source'able init scripts */
867 r = service_load_sysv_path(s, path);
872 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
873 /* Try SUSE style boot.xxx init scripts */
875 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
878 path[strlen(path)-8] = 0;
879 r = service_load_sysv_path(s, path);
886 if ((s->meta.load_state != UNIT_STUB))
893 static int service_load_sysv(Service *s) {
900 /* Load service data from SysV init scripts, preferably with
903 if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
906 if ((t = s->meta.id))
907 if ((r = service_load_sysv_name(s, t)) < 0)
910 if (s->meta.load_state == UNIT_STUB)
911 SET_FOREACH(t, s->meta.names, i) {
915 if ((r = service_load_sysv_name(s, t)) < 0)
918 if (s->meta.load_state != UNIT_STUB)
926 static int service_verify(Service *s) {
929 if (s->meta.load_state != UNIT_LOADED)
932 if (!s->exec_command[SERVICE_EXEC_START]) {
933 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
937 if (s->type != SERVICE_ONESHOT &&
938 s->exec_command[SERVICE_EXEC_START]->command_next) {
939 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
943 if (s->type == SERVICE_DBUS && !s->bus_name) {
944 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
948 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
949 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
956 static int service_add_default_dependencies(Service *s) {
961 /* Add a number of automatic dependencies useful for the
962 * majority of services. */
964 /* First, pull in base system */
965 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
967 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
970 } else if (s->meta.manager->running_as == MANAGER_SESSION) {
972 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
976 /* Second, activate normal shutdown */
977 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
980 static int service_load(Unit *u) {
982 Service *s = SERVICE(u);
986 /* Load a .service file */
987 if ((r = unit_load_fragment(u)) < 0)
990 #ifdef HAVE_SYSV_COMPAT
991 /* Load a classic init script as a fallback, if we couldn't find anything */
992 if (u->meta.load_state == UNIT_STUB)
993 if ((r = service_load_sysv(s)) < 0)
997 /* Still nothing found? Then let's give up */
998 if (u->meta.load_state == UNIT_STUB)
1001 /* We were able to load something, then let's add in the
1002 * dropin directories. */
1003 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1006 /* This is a new unit? Then let's add in some extras */
1007 if (u->meta.load_state == UNIT_LOADED) {
1008 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1011 if ((r = unit_add_default_cgroup(u)) < 0)
1014 #ifdef HAVE_SYSV_COMPAT
1015 if ((r = sysv_fix_order(s)) < 0)
1020 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1023 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1024 s->notify_access = NOTIFY_MAIN;
1026 if (s->type == SERVICE_DBUS || s->bus_name)
1027 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
1030 if (s->meta.default_dependencies)
1031 if ((r = service_add_default_dependencies(s)) < 0)
1035 return service_verify(s);
1038 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1040 ServiceExecCommand c;
1041 Service *s = SERVICE(u);
1042 const char *prefix2;
1047 p2 = strappend(prefix, "\t");
1048 prefix2 = p2 ? p2 : prefix;
1051 "%sService State: %s\n"
1052 "%sPermissionsStartOnly: %s\n"
1053 "%sRootDirectoryStartOnly: %s\n"
1054 "%sRemainAfterExit: %s\n"
1057 "%sNotifyAccess: %s\n",
1058 prefix, service_state_to_string(s->state),
1059 prefix, yes_no(s->permissions_start_only),
1060 prefix, yes_no(s->root_directory_start_only),
1061 prefix, yes_no(s->remain_after_exit),
1062 prefix, service_type_to_string(s->type),
1063 prefix, service_restart_to_string(s->restart),
1064 prefix, notify_access_to_string(s->notify_access));
1066 if (s->control_pid > 0)
1068 "%sControl PID: %lu\n",
1069 prefix, (unsigned long) s->control_pid);
1071 if (s->main_pid > 0)
1073 "%sMain PID: %lu\n",
1074 prefix, (unsigned long) s->main_pid);
1079 prefix, s->pid_file);
1084 "%sBus Name Good: %s\n",
1085 prefix, s->bus_name,
1086 prefix, yes_no(s->bus_name_good));
1088 exec_context_dump(&s->exec_context, f, prefix);
1090 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1092 if (!s->exec_command[c])
1095 fprintf(f, "%s-> %s:\n",
1096 prefix, service_exec_command_to_string(c));
1098 exec_command_dump_list(s->exec_command[c], f, prefix2);
1101 #ifdef HAVE_SYSV_COMPAT
1104 "%sSysV Init Script Path: %s\n"
1105 "%sSysV Init Script has LSB Header: %s\n",
1106 prefix, s->sysv_path,
1107 prefix, yes_no(s->sysv_has_lsb));
1109 if (s->sysv_start_priority >= 0)
1111 "%sSysVStartPriority: %i\n"
1112 "%sSysVEnabled: %s\n",
1113 prefix, s->sysv_start_priority,
1114 prefix, yes_no(s->sysv_enabled));
1116 if (s->sysv_runlevels)
1117 fprintf(f, "%sSysVRunLevels: %s\n",
1118 prefix, s->sysv_runlevels);
1122 fprintf(f, "%sStatus Text: %s\n",
1123 prefix, s->status_text);
1128 static int service_load_pid_file(Service *s) {
1135 if (s->main_pid_known)
1138 assert(s->main_pid <= 0);
1143 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1146 r = parse_pid(k, &pid);
1152 if (kill(pid, 0) < 0 && errno != EPERM) {
1153 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1154 (unsigned long) pid, s->pid_file);
1158 if ((r = service_set_main_pid(s, pid)) < 0)
1161 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1162 /* FIXME: we need to do something here */
1168 static int service_get_sockets(Service *s, Set **_set) {
1177 if (s->socket_fd >= 0)
1180 /* Collects all Socket objects that belong to this
1181 * service. Note that a service might have multiple sockets
1182 * via multiple names. */
1184 if (!(set = set_new(NULL, NULL)))
1187 SET_FOREACH(t, s->meta.names, i) {
1191 /* Look for all socket objects that go by any of our
1192 * units and collect their fds */
1194 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1199 p = manager_get_unit(s->meta.manager, k);
1205 if ((r = set_put(set, p)) < 0)
1217 static int service_notify_sockets_dead(Service *s) {
1225 if (s->socket_fd >= 0)
1228 /* Notifies all our sockets when we die */
1229 if ((r = service_get_sockets(s, &set)) < 0)
1232 SET_FOREACH(sock, set, i)
1233 socket_notify_service_dead(sock);
1240 static void service_set_state(Service *s, ServiceState state) {
1241 ServiceState old_state;
1244 old_state = s->state;
1247 if (state != SERVICE_START_PRE &&
1248 state != SERVICE_START &&
1249 state != SERVICE_START_POST &&
1250 state != SERVICE_RELOAD &&
1251 state != SERVICE_STOP &&
1252 state != SERVICE_STOP_SIGTERM &&
1253 state != SERVICE_STOP_SIGKILL &&
1254 state != SERVICE_STOP_POST &&
1255 state != SERVICE_FINAL_SIGTERM &&
1256 state != SERVICE_FINAL_SIGKILL &&
1257 state != SERVICE_AUTO_RESTART)
1258 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1260 if (state != SERVICE_START &&
1261 state != SERVICE_START_POST &&
1262 state != SERVICE_RUNNING &&
1263 state != SERVICE_RELOAD &&
1264 state != SERVICE_STOP &&
1265 state != SERVICE_STOP_SIGTERM &&
1266 state != SERVICE_STOP_SIGKILL)
1267 service_unwatch_main_pid(s);
1269 if (state != SERVICE_START_PRE &&
1270 state != SERVICE_START &&
1271 state != SERVICE_START_POST &&
1272 state != SERVICE_RELOAD &&
1273 state != SERVICE_STOP &&
1274 state != SERVICE_STOP_SIGTERM &&
1275 state != SERVICE_STOP_SIGKILL &&
1276 state != SERVICE_STOP_POST &&
1277 state != SERVICE_FINAL_SIGTERM &&
1278 state != SERVICE_FINAL_SIGKILL) {
1279 service_unwatch_control_pid(s);
1280 s->control_command = NULL;
1281 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1284 if (state == SERVICE_DEAD ||
1285 state == SERVICE_STOP ||
1286 state == SERVICE_STOP_SIGTERM ||
1287 state == SERVICE_STOP_SIGKILL ||
1288 state == SERVICE_STOP_POST ||
1289 state == SERVICE_FINAL_SIGTERM ||
1290 state == SERVICE_FINAL_SIGKILL ||
1291 state == SERVICE_FAILED ||
1292 state == SERVICE_AUTO_RESTART)
1293 service_notify_sockets_dead(s);
1295 if (state != SERVICE_START_PRE &&
1296 state != SERVICE_START &&
1297 state != SERVICE_START_POST &&
1298 state != SERVICE_RUNNING &&
1299 state != SERVICE_RELOAD &&
1300 state != SERVICE_STOP &&
1301 state != SERVICE_STOP_SIGTERM &&
1302 state != SERVICE_STOP_SIGKILL &&
1303 state != SERVICE_STOP_POST &&
1304 state != SERVICE_FINAL_SIGTERM &&
1305 state != SERVICE_FINAL_SIGKILL &&
1306 !(state == SERVICE_DEAD && s->meta.job)) {
1307 service_close_socket_fd(s);
1308 service_connection_unref(s);
1311 /* For the inactive states unit_notify() will trim the cgroup,
1312 * but for exit we have to do that ourselves... */
1313 if (state == SERVICE_EXITED)
1314 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1316 if (old_state != state)
1317 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1319 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1322 static int service_coldplug(Unit *u) {
1323 Service *s = SERVICE(u);
1327 assert(s->state == SERVICE_DEAD);
1329 if (s->deserialized_state != s->state) {
1331 if (s->deserialized_state == SERVICE_START_PRE ||
1332 s->deserialized_state == SERVICE_START ||
1333 s->deserialized_state == SERVICE_START_POST ||
1334 s->deserialized_state == SERVICE_RELOAD ||
1335 s->deserialized_state == SERVICE_STOP ||
1336 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1337 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1338 s->deserialized_state == SERVICE_STOP_POST ||
1339 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1340 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1341 s->deserialized_state == SERVICE_AUTO_RESTART) {
1343 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1346 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1348 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1353 if ((s->deserialized_state == SERVICE_START &&
1354 (s->type == SERVICE_FORKING ||
1355 s->type == SERVICE_DBUS ||
1356 s->type == SERVICE_ONESHOT ||
1357 s->type == SERVICE_NOTIFY)) ||
1358 s->deserialized_state == SERVICE_START_POST ||
1359 s->deserialized_state == SERVICE_RUNNING ||
1360 s->deserialized_state == SERVICE_RELOAD ||
1361 s->deserialized_state == SERVICE_STOP ||
1362 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1363 s->deserialized_state == SERVICE_STOP_SIGKILL)
1364 if (s->main_pid > 0)
1365 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1368 if (s->deserialized_state == SERVICE_START_PRE ||
1369 s->deserialized_state == SERVICE_START ||
1370 s->deserialized_state == SERVICE_START_POST ||
1371 s->deserialized_state == SERVICE_RELOAD ||
1372 s->deserialized_state == SERVICE_STOP ||
1373 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1374 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1375 s->deserialized_state == SERVICE_STOP_POST ||
1376 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1377 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1378 if (s->control_pid > 0)
1379 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1382 service_set_state(s, s->deserialized_state);
1388 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1392 unsigned rn_fds = 0;
1400 if (s->socket_fd >= 0)
1403 if ((r = service_get_sockets(s, &set)) < 0)
1406 SET_FOREACH(sock, set, i) {
1410 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1422 if (!(t = new(int, rn_fds+cn_fds))) {
1428 memcpy(t, rfds, rn_fds);
1429 memcpy(t+rn_fds, cfds, cn_fds);
1434 rn_fds = rn_fds+cn_fds;
1452 static int service_spawn(
1457 bool apply_permissions,
1459 bool apply_tty_stdin,
1460 bool set_notify_socket,
1465 int *fds = NULL, *fdsbuf = NULL;
1466 unsigned n_fds = 0, n_env = 0;
1467 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1474 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1475 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1476 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1478 if (s->socket_fd >= 0) {
1479 fds = &s->socket_fd;
1482 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1489 if (timeout && s->timeout_usec) {
1490 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1493 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1495 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1500 if (!(our_env = new0(char*, 3))) {
1505 if (set_notify_socket)
1506 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1511 if (s->main_pid > 0)
1512 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1517 if (!(final_env = strv_env_merge(2,
1518 s->meta.manager->environment,
1533 s->meta.manager->confirm_spawn,
1534 s->meta.cgroup_bondings,
1541 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1542 /* FIXME: we need to do something here */
1548 strv_free(final_env);
1558 strv_free(final_env);
1561 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1566 static int main_pid_good(Service *s) {
1569 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1572 /* If we know the pid file, then lets just check if it is
1574 if (s->main_pid_known)
1575 return s->main_pid > 0;
1577 /* We don't know the pid */
1581 static int control_pid_good(Service *s) {
1584 return s->control_pid > 0;
1587 static int cgroup_good(Service *s) {
1592 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1598 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1605 if (allow_restart &&
1606 !s->forbid_restart &&
1607 (s->restart == SERVICE_RESTART_ALWAYS ||
1608 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1610 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1613 service_set_state(s, SERVICE_AUTO_RESTART);
1615 service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD);
1617 s->forbid_restart = false;
1622 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1623 service_enter_dead(s, false, false);
1626 static void service_enter_signal(Service *s, ServiceState state, bool success);
1628 static void service_enter_stop_post(Service *s, bool success) {
1635 service_unwatch_control_pid(s);
1637 s->control_command_id = SERVICE_EXEC_STOP_POST;
1638 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1639 if ((r = service_spawn(s,
1643 !s->permissions_start_only,
1644 !s->root_directory_start_only,
1647 &s->control_pid)) < 0)
1651 service_set_state(s, SERVICE_STOP_POST);
1653 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1658 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1659 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1662 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1664 Set *pid_set = NULL;
1665 bool wait_for_exit = false;
1672 if (s->exec_context.kill_mode != KILL_NONE) {
1673 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1675 if (s->main_pid > 0) {
1676 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1678 s->main_pid, sig) < 0 && errno != ESRCH)
1680 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1682 wait_for_exit = true;
1685 if (s->control_pid > 0) {
1686 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1688 s->control_pid, sig) < 0 && errno != ESRCH)
1690 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1692 wait_for_exit = true;
1695 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1697 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1702 /* Exclude the main/control pids from being killed via the cgroup */
1703 if (s->main_pid > 0)
1704 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1707 if (s->control_pid > 0)
1708 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1711 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
1712 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1713 log_warning("Failed to kill control group: %s", strerror(-r));
1715 wait_for_exit = true;
1721 if (wait_for_exit) {
1722 if (s->timeout_usec > 0)
1723 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1726 service_set_state(s, state);
1727 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1728 service_enter_stop_post(s, true);
1730 service_enter_dead(s, true, true);
1735 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1737 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1738 service_enter_stop_post(s, false);
1740 service_enter_dead(s, false, true);
1746 static void service_enter_stop(Service *s, bool success) {
1754 service_unwatch_control_pid(s);
1756 s->control_command_id = SERVICE_EXEC_STOP;
1757 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1758 if ((r = service_spawn(s,
1762 !s->permissions_start_only,
1763 !s->root_directory_start_only,
1766 &s->control_pid)) < 0)
1769 service_set_state(s, SERVICE_STOP);
1771 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1776 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1777 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1780 static void service_enter_running(Service *s, bool success) {
1781 int main_pid_ok, cgroup_ok;
1787 main_pid_ok = main_pid_good(s);
1788 cgroup_ok = cgroup_good(s);
1790 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1791 (s->bus_name_good || s->type != SERVICE_DBUS))
1792 service_set_state(s, SERVICE_RUNNING);
1793 else if (s->remain_after_exit)
1794 service_set_state(s, SERVICE_EXITED);
1796 service_enter_stop(s, true);
1799 static void service_enter_start_post(Service *s) {
1803 service_unwatch_control_pid(s);
1805 s->control_command_id = SERVICE_EXEC_START_POST;
1806 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
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_POST);
1820 service_enter_running(s, true);
1825 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1826 service_enter_stop(s, false);
1829 static void service_enter_start(Service *s) {
1835 assert(s->exec_command[SERVICE_EXEC_START]);
1836 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1838 if (s->type == SERVICE_FORKING)
1839 service_unwatch_control_pid(s);
1841 service_unwatch_main_pid(s);
1843 s->control_command_id = SERVICE_EXEC_START;
1844 s->control_command = s->exec_command[SERVICE_EXEC_START];
1846 if ((r = service_spawn(s,
1848 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1853 s->notify_access != NOTIFY_NONE,
1857 if (s->type == SERVICE_SIMPLE) {
1858 /* For simple services we immediately start
1859 * the START_POST binaries. */
1861 service_set_main_pid(s, pid);
1862 service_enter_start_post(s);
1864 } else if (s->type == SERVICE_FORKING) {
1866 /* For forking services we wait until the start
1867 * process exited. */
1869 s->control_pid = pid;
1870 service_set_state(s, SERVICE_START);
1872 } else if (s->type == SERVICE_ONESHOT ||
1873 s->type == SERVICE_DBUS ||
1874 s->type == SERVICE_NOTIFY) {
1876 /* For oneshot services we wait until the start
1877 * process exited, too, but it is our main process. */
1879 /* For D-Bus services we know the main pid right away,
1880 * but wait for the bus name to appear on the
1881 * bus. Notify services are similar. */
1883 service_set_main_pid(s, pid);
1884 service_set_state(s, SERVICE_START);
1886 assert_not_reached("Unknown service type");
1891 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1892 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1895 static void service_enter_start_pre(Service *s) {
1900 service_unwatch_control_pid(s);
1902 s->control_command_id = SERVICE_EXEC_START_PRE;
1903 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1904 if ((r = service_spawn(s,
1908 !s->permissions_start_only,
1909 !s->root_directory_start_only,
1912 &s->control_pid)) < 0)
1915 service_set_state(s, SERVICE_START_PRE);
1917 service_enter_start(s);
1922 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1923 service_enter_dead(s, false, true);
1926 static void service_enter_restart(Service *s) {
1931 dbus_error_init(&error);
1933 service_enter_dead(s, true, false);
1935 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
1938 log_debug("%s scheduled restart job.", s->meta.id);
1942 log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
1943 service_enter_dead(s, false, false);
1945 dbus_error_free(&error);
1948 static void service_enter_reload(Service *s) {
1953 service_unwatch_control_pid(s);
1955 s->control_command_id = SERVICE_EXEC_RELOAD;
1956 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1957 if ((r = service_spawn(s,
1961 !s->permissions_start_only,
1962 !s->root_directory_start_only,
1965 &s->control_pid)) < 0)
1968 service_set_state(s, SERVICE_RELOAD);
1970 service_enter_running(s, true);
1975 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1976 service_enter_stop(s, false);
1979 static void service_run_next_control(Service *s, bool success) {
1983 assert(s->control_command);
1984 assert(s->control_command->command_next);
1989 assert(s->control_command_id != SERVICE_EXEC_START);
1991 s->control_command = s->control_command->command_next;
1992 service_unwatch_control_pid(s);
1994 if ((r = service_spawn(s,
1998 !s->permissions_start_only,
1999 !s->root_directory_start_only,
2000 s->control_command_id == SERVICE_EXEC_START_PRE ||
2001 s->control_command_id == SERVICE_EXEC_STOP_POST,
2003 &s->control_pid)) < 0)
2009 log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
2011 if (s->state == SERVICE_START_PRE)
2012 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2013 else if (s->state == SERVICE_STOP)
2014 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2015 else if (s->state == SERVICE_STOP_POST)
2016 service_enter_dead(s, false, true);
2018 service_enter_stop(s, false);
2021 static void service_run_next_main(Service *s, bool success) {
2026 assert(s->control_command);
2027 assert(s->control_command->command_next);
2032 assert(s->control_command_id == SERVICE_EXEC_START);
2033 assert(s->type == SERVICE_ONESHOT);
2035 s->control_command = s->control_command->command_next;
2036 service_unwatch_main_pid(s);
2038 if ((r = service_spawn(s,
2045 s->notify_access != NOTIFY_NONE,
2049 service_set_main_pid(s, pid);
2054 log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
2055 service_enter_stop(s, false);
2058 static int service_start(Unit *u) {
2059 Service *s = SERVICE(u);
2063 /* We cannot fulfill this request right now, try again later
2065 if (s->state == SERVICE_STOP ||
2066 s->state == SERVICE_STOP_SIGTERM ||
2067 s->state == SERVICE_STOP_SIGKILL ||
2068 s->state == SERVICE_STOP_POST ||
2069 s->state == SERVICE_FINAL_SIGTERM ||
2070 s->state == SERVICE_FINAL_SIGKILL)
2073 /* Already on it! */
2074 if (s->state == SERVICE_START_PRE ||
2075 s->state == SERVICE_START ||
2076 s->state == SERVICE_START_POST)
2079 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
2081 /* Make sure we don't enter a busy loop of some kind. */
2082 if (!ratelimit_test(&s->ratelimit)) {
2083 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
2087 if ((s->exec_context.std_input == EXEC_INPUT_SOCKET ||
2088 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
2089 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) &&
2091 log_warning("%s can only be started with a per-connection socket.", u->meta.id);
2096 s->main_pid_known = false;
2097 s->forbid_restart = false;
2099 service_enter_start_pre(s);
2103 static int service_stop(Unit *u) {
2104 Service *s = SERVICE(u);
2108 /* This is a user request, so don't do restarts on this
2110 s->forbid_restart = true;
2113 if (s->state == SERVICE_STOP ||
2114 s->state == SERVICE_STOP_SIGTERM ||
2115 s->state == SERVICE_STOP_SIGKILL ||
2116 s->state == SERVICE_STOP_POST ||
2117 s->state == SERVICE_FINAL_SIGTERM ||
2118 s->state == SERVICE_FINAL_SIGKILL)
2121 /* Don't allow a restart */
2122 if (s->state == SERVICE_AUTO_RESTART) {
2123 service_set_state(s, SERVICE_DEAD);
2127 /* If there's already something running we go directly into
2129 if (s->state == SERVICE_START_PRE ||
2130 s->state == SERVICE_START ||
2131 s->state == SERVICE_START_POST ||
2132 s->state == SERVICE_RELOAD) {
2133 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2137 assert(s->state == SERVICE_RUNNING ||
2138 s->state == SERVICE_EXITED);
2140 service_enter_stop(s, true);
2144 static int service_reload(Unit *u) {
2145 Service *s = SERVICE(u);
2149 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2151 service_enter_reload(s);
2155 static bool service_can_reload(Unit *u) {
2156 Service *s = SERVICE(u);
2160 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2163 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2164 Service *s = SERVICE(u);
2170 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2171 unit_serialize_item(u, f, "failure", yes_no(s->failure));
2173 if (s->control_pid > 0)
2174 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2176 if (s->main_pid_known && s->main_pid > 0)
2177 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2179 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2182 unit_serialize_item(u, f, "status-text", s->status_text);
2184 /* There's a minor uncleanliness here: if there are multiple
2185 * commands attached here, we will start from the first one
2187 if (s->control_command_id >= 0)
2188 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2190 if (s->socket_fd >= 0) {
2193 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2196 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2199 if (s->main_exec_status.pid > 0) {
2200 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2202 if (s->main_exec_status.start_timestamp.realtime > 0) {
2203 unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2204 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2206 unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2207 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2210 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2211 unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2212 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2213 unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2214 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2216 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2217 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2224 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2225 Service *s = SERVICE(u);
2232 if (streq(key, "state")) {
2235 if ((state = service_state_from_string(value)) < 0)
2236 log_debug("Failed to parse state value %s", value);
2238 s->deserialized_state = state;
2239 } else if (streq(key, "failure")) {
2242 if ((b = parse_boolean(value)) < 0)
2243 log_debug("Failed to parse failure value %s", value);
2245 s->failure = b || s->failure;
2246 } else if (streq(key, "control-pid")) {
2249 if (parse_pid(value, &pid) < 0)
2250 log_debug("Failed to parse control-pid value %s", value);
2252 s->control_pid = pid;
2253 } else if (streq(key, "main-pid")) {
2256 if (parse_pid(value, &pid) < 0)
2257 log_debug("Failed to parse main-pid value %s", value);
2259 service_set_main_pid(s, (pid_t) pid);
2260 } else if (streq(key, "main-pid-known")) {
2263 if ((b = parse_boolean(value)) < 0)
2264 log_debug("Failed to parse main-pid-known value %s", value);
2266 s->main_pid_known = b;
2267 } else if (streq(key, "status-text")) {
2270 if ((t = strdup(value))) {
2271 free(s->status_text);
2275 } else if (streq(key, "control-command")) {
2276 ServiceExecCommand id;
2278 if ((id = service_exec_command_from_string(value)) < 0)
2279 log_debug("Failed to parse exec-command value %s", value);
2281 s->control_command_id = id;
2282 s->control_command = s->exec_command[id];
2284 } else if (streq(key, "socket-fd")) {
2287 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2288 log_debug("Failed to parse socket-fd value %s", value);
2291 if (s->socket_fd >= 0)
2292 close_nointr_nofail(s->socket_fd);
2293 s->socket_fd = fdset_remove(fds, fd);
2295 } else if (streq(key, "main-exec-status-pid")) {
2298 if (parse_pid(value, &pid) < 0)
2299 log_debug("Failed to parse main-exec-status-pid value %s", value);
2301 s->main_exec_status.pid = pid;
2302 } else if (streq(key, "main-exec-status-code")) {
2305 if (safe_atoi(value, &i) < 0)
2306 log_debug("Failed to parse main-exec-status-code value %s", value);
2308 s->main_exec_status.code = i;
2309 } else if (streq(key, "main-exec-status-status")) {
2312 if (safe_atoi(value, &i) < 0)
2313 log_debug("Failed to parse main-exec-status-status value %s", value);
2315 s->main_exec_status.status = i;
2316 } else if (streq(key, "main-exec-status-start-realtime")) {
2319 if (safe_atou64(value, &k) < 0)
2320 log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2322 s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2323 } else if (streq(key, "main-exec-status-start-monotonic")) {
2326 if (safe_atou64(value, &k) < 0)
2327 log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2329 s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2330 } else if (streq(key, "main-exec-status-exit-realtime")) {
2333 if (safe_atou64(value, &k) < 0)
2334 log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2336 s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2337 } else if (streq(key, "main-exec-status-exit-monotonic")) {
2340 if (safe_atou64(value, &k) < 0)
2341 log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2343 s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2345 log_debug("Unknown serialization key '%s'", key);
2350 static UnitActiveState service_active_state(Unit *u) {
2353 return state_translation_table[SERVICE(u)->state];
2356 static const char *service_sub_state_to_string(Unit *u) {
2359 return service_state_to_string(SERVICE(u)->state);
2362 #ifdef HAVE_SYSV_COMPAT
2363 static bool service_check_gc(Unit *u) {
2364 Service *s = SERVICE(u);
2368 return !!s->sysv_path;
2372 static bool service_check_snapshot(Unit *u) {
2373 Service *s = SERVICE(u);
2377 return !s->got_socket_fd;
2380 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2381 Service *s = SERVICE(u);
2387 if (!s->meta.fragment_path)
2388 success = is_clean_exit_lsb(code, status);
2390 success = is_clean_exit(code, status);
2392 if (s->main_pid == pid) {
2395 exec_status_exit(&s->main_exec_status, pid, code, status);
2397 if (s->type != SERVICE_FORKING && s->control_command) {
2398 s->control_command->exec_status = s->main_exec_status;
2400 if (s->control_command->ignore)
2404 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2405 "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2406 s->failure = s->failure || !success;
2408 if (s->control_command &&
2409 s->control_command->command_next &&
2412 /* There is another command to *
2413 * execute, so let's do that. */
2415 log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2416 service_run_next_main(s, success);
2420 /* The service exited, so the service is officially
2423 s->control_command = NULL;
2424 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2428 case SERVICE_START_POST:
2429 case SERVICE_RELOAD:
2431 /* Need to wait until the operation is
2436 if (s->type == SERVICE_ONESHOT) {
2437 /* This was our main goal, so let's go on */
2439 service_enter_start_post(s);
2441 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2444 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2449 case SERVICE_RUNNING:
2450 service_enter_running(s, success);
2453 case SERVICE_STOP_SIGTERM:
2454 case SERVICE_STOP_SIGKILL:
2456 if (!control_pid_good(s))
2457 service_enter_stop_post(s, success);
2459 /* If there is still a control process, wait for that first */
2463 assert_not_reached("Uh, main process died at wrong time.");
2467 } else if (s->control_pid == pid) {
2471 if (s->control_command) {
2472 exec_status_exit(&s->control_command->exec_status, pid, code, status);
2474 if (s->control_command->ignore)
2478 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2479 "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2480 s->failure = s->failure || !success;
2482 if (s->control_command &&
2483 s->control_command->command_next &&
2486 /* There is another command to *
2487 * execute, so let's do that. */
2489 log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2490 service_run_next_control(s, success);
2493 /* No further commands for this step, so let's
2494 * figure out what to do next */
2496 s->control_command = NULL;
2497 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2499 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2503 case SERVICE_START_PRE:
2505 service_enter_start(s);
2507 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2511 assert(s->type == SERVICE_FORKING);
2513 /* Let's try to load the pid
2514 * file here if we can. We
2515 * ignore the return value,
2516 * since the PID file might
2517 * actually be created by a
2518 * START_POST script */
2522 service_load_pid_file(s);
2524 service_enter_start_post(s);
2526 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2530 case SERVICE_START_POST:
2531 if (success && s->pid_file && !s->main_pid_known) {
2534 /* Hmm, let's see if we can
2535 * load the pid now after the
2536 * start-post scripts got
2539 if ((r = service_load_pid_file(s)) < 0)
2540 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2545 case SERVICE_RELOAD:
2547 service_enter_running(s, true);
2549 service_enter_stop(s, false);
2554 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2557 case SERVICE_STOP_SIGTERM:
2558 case SERVICE_STOP_SIGKILL:
2559 if (main_pid_good(s) <= 0)
2560 service_enter_stop_post(s, success);
2562 /* If there is still a service
2563 * process around, wait until
2564 * that one quit, too */
2567 case SERVICE_STOP_POST:
2568 case SERVICE_FINAL_SIGTERM:
2569 case SERVICE_FINAL_SIGKILL:
2570 service_enter_dead(s, success, true);
2574 assert_not_reached("Uh, control process died at wrong time.");
2579 /* Notify clients about changed exit status */
2580 unit_add_to_dbus_queue(u);
2583 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2584 Service *s = SERVICE(u);
2587 assert(elapsed == 1);
2589 assert(w == &s->timer_watch);
2593 case SERVICE_START_PRE:
2595 log_warning("%s operation timed out. Terminating.", u->meta.id);
2596 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2599 case SERVICE_START_POST:
2600 case SERVICE_RELOAD:
2601 log_warning("%s operation timed out. Stopping.", u->meta.id);
2602 service_enter_stop(s, false);
2606 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2607 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2610 case SERVICE_STOP_SIGTERM:
2611 log_warning("%s stopping timed out. Killing.", u->meta.id);
2612 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2615 case SERVICE_STOP_SIGKILL:
2616 /* Uh, wie sent a SIGKILL and it is still not gone?
2617 * Must be something we cannot kill, so let's just be
2618 * weirded out and continue */
2620 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2621 service_enter_stop_post(s, false);
2624 case SERVICE_STOP_POST:
2625 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2626 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2629 case SERVICE_FINAL_SIGTERM:
2630 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2631 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2634 case SERVICE_FINAL_SIGKILL:
2635 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
2636 service_enter_dead(s, false, true);
2639 case SERVICE_AUTO_RESTART:
2640 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2641 service_enter_restart(s);
2645 assert_not_reached("Timeout at wrong time.");
2649 static void service_cgroup_notify_event(Unit *u) {
2650 Service *s = SERVICE(u);
2654 log_debug("%s: cgroup is empty", u->meta.id);
2658 /* Waiting for SIGCHLD is usually more interesting,
2659 * because it includes return codes/signals. Which is
2660 * why we ignore the cgroup events for most cases,
2661 * except when we don't know pid which to expect the
2664 case SERVICE_RUNNING:
2665 service_enter_running(s, true);
2668 case SERVICE_STOP_SIGTERM:
2669 case SERVICE_STOP_SIGKILL:
2670 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2671 service_enter_stop_post(s, true);
2675 case SERVICE_FINAL_SIGTERM:
2676 case SERVICE_FINAL_SIGKILL:
2677 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2678 service_enter_dead(s, true, true);
2687 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2688 Service *s = SERVICE(u);
2693 if (s->notify_access == NOTIFY_NONE) {
2694 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2695 u->meta.id, (unsigned long) pid);
2699 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2700 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2701 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2705 log_debug("%s: Got message", u->meta.id);
2707 /* Interpret MAINPID= */
2708 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2709 (s->state == SERVICE_START ||
2710 s->state == SERVICE_START_POST ||
2711 s->state == SERVICE_RUNNING ||
2712 s->state == SERVICE_RELOAD)) {
2714 if (parse_pid(e + 8, &pid) < 0)
2715 log_warning("Failed to parse notification message %s", e);
2717 log_debug("%s: got %s", u->meta.id, e);
2718 service_set_main_pid(s, pid);
2722 /* Interpret READY= */
2723 if (s->type == SERVICE_NOTIFY &&
2724 s->state == SERVICE_START &&
2725 strv_find(tags, "READY=1")) {
2726 log_debug("%s: got READY=1", u->meta.id);
2728 service_enter_start_post(s);
2731 /* Interpret STATUS= */
2732 if ((e = strv_find_prefix(tags, "STATUS="))) {
2736 if (!(t = strdup(e+7))) {
2737 log_error("Failed to allocate string.");
2741 log_debug("%s: got %s", u->meta.id, e);
2743 free(s->status_text);
2746 free(s->status_text);
2747 s->status_text = NULL;
2752 /* Notify clients about changed status or main pid */
2753 unit_add_to_dbus_queue(u);
2756 #ifdef HAVE_SYSV_COMPAT
2757 static int service_enumerate(Manager *m) {
2761 char *path = NULL, *fpath = NULL, *name = NULL;
2762 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
2767 Unit *previous = NULL;
2768 char *arch_daemons = NULL;
2769 char *arch_daemons_stripped = NULL;
2770 char **arch_daemons_split = NULL;
2776 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
2777 "DAEMONS", &arch_daemons,
2781 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
2783 } else if (arch_daemons) {
2784 if (!(arch_daemons_stripped = strchr(arch_daemons, '(')))
2785 arch_daemons_stripped = arch_daemons;
2787 arch_daemons_stripped++; /* strip start paren */
2789 arch_daemons_stripped[strcspn(arch_daemons_stripped, ")")] = 0; /* strip end paren */
2791 if (!(arch_daemons_split = strv_split_quoted(arch_daemons_stripped))) {
2796 STRV_FOREACH(p, arch_daemons_split) {
2801 if (**p == '!') /* daemons prefixed with ! are disabled, so ignore them */
2804 if (!(name = sysv_translate_name(*p))) {
2809 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2810 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2814 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, "multi-user.target", NULL, true)) < 0)
2818 if ((r = unit_add_dependency(service, UNIT_AFTER, previous, true)) < 0)
2821 if (**p != '@') /* daemons prefixed with @ can be started in the background */
2827 zero(runlevel_services);
2829 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2830 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2835 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2843 if (!(d = opendir(path))) {
2844 if (errno != ENOENT)
2845 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2850 while ((de = readdir(d))) {
2853 if (ignore_file(de->d_name))
2856 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2859 if (strlen(de->d_name) < 4)
2862 a = undecchar(de->d_name[1]);
2863 b = undecchar(de->d_name[2]);
2870 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2875 if (access(fpath, X_OK) < 0) {
2877 if (errno != ENOENT)
2878 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2884 if (!(name = sysv_translate_name(de->d_name + 3))) {
2889 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2890 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2894 if (de->d_name[0] == 'S') {
2896 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
2897 SERVICE(service)->sysv_start_priority =
2898 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2900 SERVICE(service)->sysv_enabled = true;
2903 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
2906 if ((r = set_put(runlevel_services[i], service)) < 0)
2909 } else if (de->d_name[0] == 'K' &&
2910 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2911 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2913 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
2916 if ((r = set_put(shutdown_services, service)) < 0)
2922 /* Now we loaded all stubs and are aware of the lowest
2923 start-up priority for all services, not let's actually load
2924 the services, this will also tell us which services are
2925 actually native now */
2926 manager_dispatch_load_queue(m);
2928 /* If this is a native service, rely on native ways to pull in
2929 * a service, don't pull it in via sysv rcN.d links. */
2930 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
2931 SET_FOREACH(service, runlevel_services[i], j) {
2932 service = unit_follow_merge(service);
2934 if (service->meta.fragment_path)
2937 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2941 /* We honour K links only for halt/reboot. For the normal
2942 * runlevels we assume the stop jobs will be implicitly added
2943 * by the core logic. Also, we don't really distuingish here
2944 * between the runlevels 0 and 6 and just add them to the
2945 * special shutdown target. On SUSE the boot.d/ runlevel is
2946 * also used for shutdown, so we add links for that too to the
2947 * shutdown target.*/
2948 SET_FOREACH(service, shutdown_services, j) {
2949 service = unit_follow_merge(service);
2951 if (service->meta.fragment_path)
2954 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2966 free(arch_daemons_split);
2969 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
2970 set_free(runlevel_services[i]);
2971 set_free(shutdown_services);
2980 static void service_bus_name_owner_change(
2983 const char *old_owner,
2984 const char *new_owner) {
2986 Service *s = SERVICE(u);
2991 assert(streq(s->bus_name, name));
2992 assert(old_owner || new_owner);
2994 if (old_owner && new_owner)
2995 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2997 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2999 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
3001 s->bus_name_good = !!new_owner;
3003 if (s->type == SERVICE_DBUS) {
3005 /* service_enter_running() will figure out what to
3007 if (s->state == SERVICE_RUNNING)
3008 service_enter_running(s, true);
3009 else if (s->state == SERVICE_START && new_owner)
3010 service_enter_start_post(s);
3012 } else if (new_owner &&
3014 (s->state == SERVICE_START ||
3015 s->state == SERVICE_START_POST ||
3016 s->state == SERVICE_RUNNING ||
3017 s->state == SERVICE_RELOAD)) {
3019 /* Try to acquire PID from bus service */
3020 log_debug("Trying to acquire PID from D-Bus name...");
3022 bus_query_pid(u->meta.manager, name);
3026 static void service_bus_query_pid_done(
3031 Service *s = SERVICE(u);
3036 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
3038 if (s->main_pid <= 0 &&
3039 (s->state == SERVICE_START ||
3040 s->state == SERVICE_START_POST ||
3041 s->state == SERVICE_RUNNING ||
3042 s->state == SERVICE_RELOAD))
3043 service_set_main_pid(s, pid);
3046 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3050 /* This is called by the socket code when instantiating a new
3051 * service for a stream socket and the socket needs to be
3054 if (s->meta.load_state != UNIT_LOADED)
3057 if (s->socket_fd >= 0)
3060 if (s->state != SERVICE_DEAD)
3064 s->got_socket_fd = true;
3070 static void service_reset_failed(Unit *u) {
3071 Service *s = SERVICE(u);
3075 if (s->state == SERVICE_FAILED)
3076 service_set_state(s, SERVICE_DEAD);
3081 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3082 [SERVICE_DEAD] = "dead",
3083 [SERVICE_START_PRE] = "start-pre",
3084 [SERVICE_START] = "start",
3085 [SERVICE_START_POST] = "start-post",
3086 [SERVICE_RUNNING] = "running",
3087 [SERVICE_EXITED] = "exited",
3088 [SERVICE_RELOAD] = "reload",
3089 [SERVICE_STOP] = "stop",
3090 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3091 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3092 [SERVICE_STOP_POST] = "stop-post",
3093 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3094 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3095 [SERVICE_FAILED] = "failed",
3096 [SERVICE_AUTO_RESTART] = "auto-restart",
3099 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3101 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3102 [SERVICE_ONCE] = "once",
3103 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
3104 [SERVICE_RESTART_ALWAYS] = "restart-always",
3107 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3109 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3110 [SERVICE_SIMPLE] = "simple",
3111 [SERVICE_FORKING] = "forking",
3112 [SERVICE_ONESHOT] = "oneshot",
3113 [SERVICE_DBUS] = "dbus",
3114 [SERVICE_NOTIFY] = "notify"
3117 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3119 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3120 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3121 [SERVICE_EXEC_START] = "ExecStart",
3122 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3123 [SERVICE_EXEC_RELOAD] = "ExecReload",
3124 [SERVICE_EXEC_STOP] = "ExecStop",
3125 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3128 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3130 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3131 [NOTIFY_NONE] = "none",
3132 [NOTIFY_MAIN] = "main",
3133 [NOTIFY_ALL] = "all"
3136 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3138 const UnitVTable service_vtable = {
3139 .suffix = ".service",
3140 .show_status = true,
3142 .init = service_init,
3143 .done = service_done,
3144 .load = service_load,
3146 .coldplug = service_coldplug,
3148 .dump = service_dump,
3150 .start = service_start,
3151 .stop = service_stop,
3152 .reload = service_reload,
3154 .can_reload = service_can_reload,
3156 .serialize = service_serialize,
3157 .deserialize_item = service_deserialize_item,
3159 .active_state = service_active_state,
3160 .sub_state_to_string = service_sub_state_to_string,
3162 #ifdef HAVE_SYSV_COMPAT
3163 .check_gc = service_check_gc,
3165 .check_snapshot = service_check_snapshot,
3167 .sigchld_event = service_sigchld_event,
3168 .timer_event = service_timer_event,
3170 .reset_failed = service_reset_failed,
3172 .cgroup_notify_empty = service_cgroup_notify_event,
3173 .notify_message = service_notify_message,
3175 .bus_name_owner_change = service_bus_name_owner_change,
3176 .bus_query_pid_done = service_bus_query_pid_done,
3178 .bus_interface = "org.freedesktop.systemd1.Service",
3179 .bus_message_handler = bus_service_message_handler,
3180 .bus_invalidating_properties = bus_service_invalidating_properties,
3182 #ifdef HAVE_SYSV_COMPAT
3183 .enumerate = service_enumerate