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
43 #define DEFAULT_SYSV_TIMEOUT_USEC (3*USEC_PER_MINUTE)
45 typedef enum RunlevelType {
54 const RunlevelType type;
56 /* Standard SysV runlevels for start-up */
57 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
58 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
59 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
60 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
61 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
64 /* SUSE style boot.d */
65 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
69 /* Debian style rcS.d */
70 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
73 /* Standard SysV runlevels for shutdown */
74 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
75 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
77 /* Note that the order here matters, as we read the
78 directories in this order, and we want to make sure that
79 sysv_start_priority is known when we first load the
80 unit. And that value we only know from S links. Hence
81 UP/SYSINIT must be read before DOWN */
84 #define RUNLEVELS_UP "12345"
85 /* #define RUNLEVELS_DOWN "06" */
86 /* #define RUNLEVELS_BOOT "bBsS" */
89 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
90 [SERVICE_DEAD] = UNIT_INACTIVE,
91 [SERVICE_START_PRE] = UNIT_ACTIVATING,
92 [SERVICE_START] = UNIT_ACTIVATING,
93 [SERVICE_START_POST] = UNIT_ACTIVATING,
94 [SERVICE_RUNNING] = UNIT_ACTIVE,
95 [SERVICE_EXITED] = UNIT_ACTIVE,
96 [SERVICE_RELOAD] = UNIT_RELOADING,
97 [SERVICE_STOP] = UNIT_DEACTIVATING,
98 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
99 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
100 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
101 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
102 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
103 [SERVICE_FAILED] = UNIT_FAILED,
104 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
107 static void service_init(Unit *u) {
108 Service *s = SERVICE(u);
111 assert(u->meta.load_state == UNIT_STUB);
113 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
114 s->restart_usec = DEFAULT_RESTART_USEC;
115 s->timer_watch.type = WATCH_INVALID;
116 #ifdef HAVE_SYSV_COMPAT
117 s->sysv_start_priority = -1;
121 exec_context_init(&s->exec_context);
123 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
125 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
128 static void service_unwatch_control_pid(Service *s) {
131 if (s->control_pid <= 0)
134 unit_unwatch_pid(UNIT(s), s->control_pid);
138 static void service_unwatch_main_pid(Service *s) {
141 if (s->main_pid <= 0)
144 unit_unwatch_pid(UNIT(s), s->main_pid);
148 static int service_set_main_pid(Service *s, pid_t pid) {
159 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
160 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
161 s->meta.id, (unsigned long) pid);
164 s->main_pid_known = true;
166 exec_status_start(&s->main_exec_status, pid);
171 static void service_close_socket_fd(Service *s) {
174 if (s->socket_fd < 0)
177 close_nointr_nofail(s->socket_fd);
181 static void service_connection_unref(Service *s) {
184 if (!s->accept_socket)
187 socket_connection_unref(s->accept_socket);
188 s->accept_socket = NULL;
191 static void service_done(Unit *u) {
192 Service *s = SERVICE(u);
199 #ifdef HAVE_SYSV_COMPAT
203 free(s->sysv_runlevels);
204 s->sysv_runlevels = NULL;
207 free(s->status_text);
208 s->status_text = NULL;
210 exec_context_done(&s->exec_context);
211 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
212 s->control_command = NULL;
214 /* This will leak a process, but at least no memory or any of
216 service_unwatch_main_pid(s);
217 service_unwatch_control_pid(s);
220 unit_unwatch_bus_name(UNIT(u), s->bus_name);
225 service_close_socket_fd(s);
226 service_connection_unref(s);
228 set_free(s->configured_sockets);
230 unit_unwatch_timer(u, &s->timer_watch);
233 #ifdef HAVE_SYSV_COMPAT
234 static char *sysv_translate_name(const char *name) {
237 if (!(r = new(char, strlen(name) + sizeof(".service"))))
240 if (startswith(name, "boot."))
241 /* Drop SuSE-style boot. prefix */
242 strcpy(stpcpy(r, name + 5), ".service");
243 else if (endswith(name, ".sh"))
244 /* Drop Debian-style .sh suffix */
245 strcpy(stpcpy(r, name) - 3, ".service");
247 else if (startswith(name, "@"))
248 /* Drop Arch-style background prefix */
249 strcpy(stpcpy(r, name + 1), ".service");
252 /* Normal init scripts */
253 strcpy(stpcpy(r, name), ".service");
258 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
260 /* We silently ignore the $ prefix here. According to the LSB
261 * spec it simply indicates whether something is a
262 * standardized name or a distribution-specific one. Since we
263 * just follow what already exists and do not introduce new
264 * uses or names we don't care who introduced a new name. */
266 static const char * const table[] = {
267 /* LSB defined facilities */
268 "local_fs", SPECIAL_LOCAL_FS_TARGET,
269 "network", SPECIAL_NETWORK_TARGET,
270 "named", SPECIAL_NSS_LOOKUP_TARGET,
271 "portmap", SPECIAL_RPCBIND_TARGET,
272 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
273 "syslog", SPECIAL_SYSLOG_TARGET,
274 "time", SPECIAL_RTC_SET_TARGET,
276 /* Debian extensions */
278 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
280 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
281 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
284 /* Fedora extensions */
285 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
286 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
287 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
290 /* SuSE extensions */
302 n = *name == '$' ? name + 1 : name;
304 for (i = 0; i < ELEMENTSOF(table); i += 2) {
306 if (!streq(table[i], n))
312 if (!(r = strdup(table[i+1])))
318 /* If we don't know this name, fallback heuristics to figure
319 * out whether something is a target or an service alias. */
322 /* Facilities starting with $ are most likely targets */
323 r = unit_name_build(n, NULL, ".target");
324 else if (filename && streq(name, filename))
325 /* Names equalling the file name of the services are redundant */
328 /* Everything else we assume to be normal service names */
329 r = sysv_translate_name(n);
342 static int sysv_fix_order(Service *s) {
348 if (s->sysv_start_priority < 0)
351 /* For each pair of services where at least one lacks a LSB
352 * header, we use the start priority value to order things. */
354 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
357 bool special_s, special_t;
359 t = (Service*) other;
364 if (t->meta.load_state != UNIT_LOADED)
367 if (t->sysv_start_priority < 0)
370 /* If both units have modern headers we don't care
371 * about the priorities */
372 if ((s->meta.fragment_path || s->sysv_has_lsb) &&
373 (t->meta.fragment_path || t->sysv_has_lsb))
376 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
377 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
379 if (special_t && !special_s)
381 else if (special_s && !special_t)
383 else if (t->sysv_start_priority < s->sysv_start_priority)
385 else if (t->sysv_start_priority > s->sysv_start_priority)
390 /* FIXME: Maybe we should compare the name here lexicographically? */
392 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
399 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
402 if (!(c = new0(ExecCommand, 1)))
405 if (!(c->path = strdup(path))) {
410 if (!(c->argv = strv_new(path, arg1, NULL))) {
419 static int sysv_exec_commands(Service *s) {
423 assert(s->sysv_path);
425 if (!(c = exec_command_new(s->sysv_path, "start")))
427 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
429 if (!(c = exec_command_new(s->sysv_path, "stop")))
431 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
433 if (!(c = exec_command_new(s->sysv_path, "reload")))
435 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
440 static int service_load_sysv_path(Service *s, const char *path) {
451 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
458 if (!(f = fopen(path, "re"))) {
459 r = errno == ENOENT ? 0 : -errno;
464 if (!(s->sysv_path = strdup(path))) {
470 char l[LINE_MAX], *t;
472 if (!fgets(l, sizeof(l), f)) {
477 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
487 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
489 s->sysv_has_lsb = true;
493 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
499 t += strspn(t, WHITESPACE);
501 if (state == NORMAL) {
503 /* Try to parse Red Hat style chkconfig headers */
505 if (startswith_no_case(t, "chkconfig:")) {
507 char runlevels[16], *k;
511 if (sscanf(t+10, "%15s %i %*i",
513 &start_priority) != 2) {
515 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
519 /* A start priority gathered from the
520 * symlink farms is preferred over the
521 * data from the LSB header. */
522 if (start_priority < 0 || start_priority > 99)
523 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
524 else if (s->sysv_start_priority < 0)
525 s->sysv_start_priority = start_priority;
527 char_array_0(runlevels);
528 k = delete_chars(runlevels, WHITESPACE "-");
533 if (!(d = strdup(k))) {
538 free(s->sysv_runlevels);
539 s->sysv_runlevels = d;
542 } else if (startswith_no_case(t, "description:")) {
544 size_t k = strlen(t);
548 if (t[k-1] == '\\') {
553 if ((j = strstrip(t+12)) && *j) {
554 if (!(d = strdup(j))) {
561 free(chkconfig_description);
562 chkconfig_description = d;
564 } else if (startswith_no_case(t, "pidfile:")) {
571 if (!path_is_absolute(fn)) {
572 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
576 if (!(fn = strdup(fn))) {
585 } else if (state == DESCRIPTION) {
587 /* Try to parse Red Hat style description
590 size_t k = strlen(t);
598 if ((j = strstrip(t)) && *j) {
601 if (chkconfig_description)
602 asprintf(&d, "%s %s", chkconfig_description, j);
611 free(chkconfig_description);
612 chkconfig_description = d;
615 } else if (state == LSB || state == LSB_DESCRIPTION) {
617 if (startswith_no_case(t, "Provides:")) {
623 FOREACH_WORD_QUOTED(w, z, t+9, i) {
626 if (!(n = strndup(w, z))) {
631 r = sysv_translate_facility(n, file_name_from_path(path), &m);
640 if (unit_name_to_type(m) == UNIT_SERVICE)
641 r = unit_add_name(u, m);
643 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
645 if (s->sysv_enabled) {
648 if ((k = unit_add_dependency_by_name_inverse(u, UNIT_WANTS, m, NULL, true)) < 0)
654 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
659 } else if (startswith_no_case(t, "Required-Start:") ||
660 startswith_no_case(t, "Should-Start:") ||
661 startswith_no_case(t, "X-Start-Before:") ||
662 startswith_no_case(t, "X-Start-After:")) {
668 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
671 if (!(n = strndup(w, z))) {
676 r = sysv_translate_facility(n, file_name_from_path(path), &m);
685 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
688 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
692 } else if (startswith_no_case(t, "Default-Start:")) {
697 k = delete_chars(t+14, WHITESPACE "-");
700 if (!(d = strdup(k))) {
705 free(s->sysv_runlevels);
706 s->sysv_runlevels = d;
709 } else if (startswith_no_case(t, "Description:")) {
712 state = LSB_DESCRIPTION;
714 if ((j = strstrip(t+12)) && *j) {
715 if (!(d = strdup(j))) {
722 free(long_description);
723 long_description = d;
725 } else if (startswith_no_case(t, "Short-Description:")) {
730 if ((j = strstrip(t+18)) && *j) {
731 if (!(d = strdup(j))) {
738 free(short_description);
739 short_description = d;
741 } else if (startswith_no_case(t, "X-Interactive:")) {
744 if ((b = parse_boolean(strstrip(t+14))) < 0) {
745 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
750 s->exec_context.std_input = EXEC_INPUT_TTY;
752 s->exec_context.std_input = EXEC_INPUT_NULL;
754 } else if (state == LSB_DESCRIPTION) {
756 if (startswith(l, "#\t") || startswith(l, "# ")) {
759 if ((j = strstrip(t)) && *j) {
762 if (long_description)
763 asprintf(&d, "%s %s", long_description, t);
772 free(long_description);
773 long_description = d;
782 if ((r = sysv_exec_commands(s)) < 0)
785 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
786 /* If there a runlevels configured for this service
787 * but none of the standard ones, then we assume this
788 * is some special kind of service (which might be
789 * needed for early boot) and don't create any links
792 s->meta.default_dependencies = false;
794 /* Don't timeout special services during boot (like fsck) */
797 s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC;
799 /* Special setting for all SysV services */
800 s->type = SERVICE_FORKING;
801 s->remain_after_exit = true;
802 s->restart = SERVICE_RESTART_NO;
803 s->exec_context.std_output =
804 (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY)
805 ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
806 s->exec_context.kill_mode = KILL_PROCESS_GROUP;
808 /* We use the long description only if
809 * no short description is set. */
811 if (short_description)
812 description = short_description;
813 else if (chkconfig_description)
814 description = chkconfig_description;
815 else if (long_description)
816 description = long_description;
823 if (!(d = strappend("LSB: ", description))) {
828 u->meta.description = d;
831 u->meta.load_state = UNIT_LOADED;
839 free(short_description);
840 free(long_description);
841 free(chkconfig_description);
846 static int service_load_sysv_name(Service *s, const char *name) {
852 /* For SysV services we strip the boot. or .sh
853 * prefixes/suffixes. */
854 if (startswith(name, "boot.") ||
855 endswith(name, ".sh.service"))
858 STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
862 if (asprintf(&path, "%s/%s", *p, name) < 0)
865 assert(endswith(path, ".service"));
866 path[strlen(path)-8] = 0;
868 r = service_load_sysv_path(s, path);
870 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
871 /* Try Debian style xxx.sh source'able init scripts */
873 r = service_load_sysv_path(s, path);
878 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
879 /* Try SUSE style boot.xxx init scripts */
881 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
884 path[strlen(path)-8] = 0;
885 r = service_load_sysv_path(s, path);
892 if ((s->meta.load_state != UNIT_STUB))
899 static int service_load_sysv(Service *s) {
906 /* Load service data from SysV init scripts, preferably with
909 if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
912 if ((t = s->meta.id))
913 if ((r = service_load_sysv_name(s, t)) < 0)
916 if (s->meta.load_state == UNIT_STUB)
917 SET_FOREACH(t, s->meta.names, i) {
921 if ((r = service_load_sysv_name(s, t)) < 0)
924 if (s->meta.load_state != UNIT_STUB)
932 static int service_verify(Service *s) {
935 if (s->meta.load_state != UNIT_LOADED)
938 if (!s->exec_command[SERVICE_EXEC_START]) {
939 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
943 if (s->type != SERVICE_ONESHOT &&
944 s->exec_command[SERVICE_EXEC_START]->command_next) {
945 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
949 if (s->type == SERVICE_DBUS && !s->bus_name) {
950 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
954 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
955 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
962 static int service_add_default_dependencies(Service *s) {
967 /* Add a number of automatic dependencies useful for the
968 * majority of services. */
970 /* First, pull in base system */
971 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
973 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
976 } else if (s->meta.manager->running_as == MANAGER_SESSION) {
978 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
982 /* Second, activate normal shutdown */
983 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
986 static int service_load(Unit *u) {
988 Service *s = SERVICE(u);
992 /* Load a .service file */
993 if ((r = unit_load_fragment(u)) < 0)
996 #ifdef HAVE_SYSV_COMPAT
997 /* Load a classic init script as a fallback, if we couldn't find anything */
998 if (u->meta.load_state == UNIT_STUB)
999 if ((r = service_load_sysv(s)) < 0)
1003 /* Still nothing found? Then let's give up */
1004 if (u->meta.load_state == UNIT_STUB)
1007 /* We were able to load something, then let's add in the
1008 * dropin directories. */
1009 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1012 /* This is a new unit? Then let's add in some extras */
1013 if (u->meta.load_state == UNIT_LOADED) {
1014 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1017 if ((r = unit_add_default_cgroup(u)) < 0)
1020 #ifdef HAVE_SYSV_COMPAT
1021 if ((r = sysv_fix_order(s)) < 0)
1026 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1029 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1030 s->notify_access = NOTIFY_MAIN;
1032 if (s->type == SERVICE_DBUS || s->bus_name)
1033 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
1036 if (s->meta.default_dependencies)
1037 if ((r = service_add_default_dependencies(s)) < 0)
1041 return service_verify(s);
1044 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1046 ServiceExecCommand c;
1047 Service *s = SERVICE(u);
1048 const char *prefix2;
1053 p2 = strappend(prefix, "\t");
1054 prefix2 = p2 ? p2 : prefix;
1057 "%sService State: %s\n"
1058 "%sPermissionsStartOnly: %s\n"
1059 "%sRootDirectoryStartOnly: %s\n"
1060 "%sRemainAfterExit: %s\n"
1063 "%sNotifyAccess: %s\n",
1064 prefix, service_state_to_string(s->state),
1065 prefix, yes_no(s->permissions_start_only),
1066 prefix, yes_no(s->root_directory_start_only),
1067 prefix, yes_no(s->remain_after_exit),
1068 prefix, service_type_to_string(s->type),
1069 prefix, service_restart_to_string(s->restart),
1070 prefix, notify_access_to_string(s->notify_access));
1072 if (s->control_pid > 0)
1074 "%sControl PID: %lu\n",
1075 prefix, (unsigned long) s->control_pid);
1077 if (s->main_pid > 0)
1079 "%sMain PID: %lu\n",
1080 prefix, (unsigned long) s->main_pid);
1085 prefix, s->pid_file);
1090 "%sBus Name Good: %s\n",
1091 prefix, s->bus_name,
1092 prefix, yes_no(s->bus_name_good));
1094 exec_context_dump(&s->exec_context, f, prefix);
1096 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1098 if (!s->exec_command[c])
1101 fprintf(f, "%s-> %s:\n",
1102 prefix, service_exec_command_to_string(c));
1104 exec_command_dump_list(s->exec_command[c], f, prefix2);
1107 #ifdef HAVE_SYSV_COMPAT
1110 "%sSysV Init Script Path: %s\n"
1111 "%sSysV Init Script has LSB Header: %s\n",
1112 prefix, s->sysv_path,
1113 prefix, yes_no(s->sysv_has_lsb));
1115 if (s->sysv_start_priority >= 0)
1117 "%sSysVStartPriority: %i\n"
1118 "%sSysVEnabled: %s\n",
1119 prefix, s->sysv_start_priority,
1120 prefix, yes_no(s->sysv_enabled));
1122 if (s->sysv_runlevels)
1123 fprintf(f, "%sSysVRunLevels: %s\n",
1124 prefix, s->sysv_runlevels);
1128 fprintf(f, "%sStatus Text: %s\n",
1129 prefix, s->status_text);
1134 static int service_load_pid_file(Service *s) {
1141 if (s->main_pid_known)
1144 assert(s->main_pid <= 0);
1149 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1152 r = parse_pid(k, &pid);
1158 if (kill(pid, 0) < 0 && errno != EPERM) {
1159 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1160 (unsigned long) pid, s->pid_file);
1164 if ((r = service_set_main_pid(s, pid)) < 0)
1167 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1168 /* FIXME: we need to do something here */
1174 static int service_get_sockets(Service *s, Set **_set) {
1183 if (s->socket_fd >= 0)
1186 if (!set_isempty(s->configured_sockets))
1189 /* Collects all Socket objects that belong to this
1190 * service. Note that a service might have multiple sockets
1191 * via multiple names. */
1193 if (!(set = set_new(NULL, NULL)))
1196 SET_FOREACH(t, s->meta.names, i) {
1200 /* Look for all socket objects that go by any of our
1201 * units and collect their fds */
1203 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1208 p = manager_get_unit(s->meta.manager, k);
1214 if ((r = set_put(set, p)) < 0)
1226 static int service_notify_sockets_dead(Service *s) {
1228 Set *set, *free_set = NULL;
1234 /* Notifies all our sockets when we die */
1236 if (s->socket_fd >= 0)
1239 if (!set_isempty(s->configured_sockets))
1240 set = s->configured_sockets;
1242 if ((r = service_get_sockets(s, &free_set)) < 0)
1248 SET_FOREACH(sock, set, i)
1249 socket_notify_service_dead(sock);
1256 static void service_set_state(Service *s, ServiceState state) {
1257 ServiceState old_state;
1260 old_state = s->state;
1263 if (state != SERVICE_START_PRE &&
1264 state != SERVICE_START &&
1265 state != SERVICE_START_POST &&
1266 state != SERVICE_RELOAD &&
1267 state != SERVICE_STOP &&
1268 state != SERVICE_STOP_SIGTERM &&
1269 state != SERVICE_STOP_SIGKILL &&
1270 state != SERVICE_STOP_POST &&
1271 state != SERVICE_FINAL_SIGTERM &&
1272 state != SERVICE_FINAL_SIGKILL &&
1273 state != SERVICE_AUTO_RESTART)
1274 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1276 if (state != SERVICE_START &&
1277 state != SERVICE_START_POST &&
1278 state != SERVICE_RUNNING &&
1279 state != SERVICE_RELOAD &&
1280 state != SERVICE_STOP &&
1281 state != SERVICE_STOP_SIGTERM &&
1282 state != SERVICE_STOP_SIGKILL)
1283 service_unwatch_main_pid(s);
1285 if (state != SERVICE_START_PRE &&
1286 state != SERVICE_START &&
1287 state != SERVICE_START_POST &&
1288 state != SERVICE_RELOAD &&
1289 state != SERVICE_STOP &&
1290 state != SERVICE_STOP_SIGTERM &&
1291 state != SERVICE_STOP_SIGKILL &&
1292 state != SERVICE_STOP_POST &&
1293 state != SERVICE_FINAL_SIGTERM &&
1294 state != SERVICE_FINAL_SIGKILL) {
1295 service_unwatch_control_pid(s);
1296 s->control_command = NULL;
1297 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1300 if (state == SERVICE_DEAD ||
1301 state == SERVICE_STOP ||
1302 state == SERVICE_STOP_SIGTERM ||
1303 state == SERVICE_STOP_SIGKILL ||
1304 state == SERVICE_STOP_POST ||
1305 state == SERVICE_FINAL_SIGTERM ||
1306 state == SERVICE_FINAL_SIGKILL ||
1307 state == SERVICE_FAILED ||
1308 state == SERVICE_AUTO_RESTART)
1309 service_notify_sockets_dead(s);
1311 if (state != SERVICE_START_PRE &&
1312 state != SERVICE_START &&
1313 state != SERVICE_START_POST &&
1314 state != SERVICE_RUNNING &&
1315 state != SERVICE_RELOAD &&
1316 state != SERVICE_STOP &&
1317 state != SERVICE_STOP_SIGTERM &&
1318 state != SERVICE_STOP_SIGKILL &&
1319 state != SERVICE_STOP_POST &&
1320 state != SERVICE_FINAL_SIGTERM &&
1321 state != SERVICE_FINAL_SIGKILL &&
1322 !(state == SERVICE_DEAD && s->meta.job)) {
1323 service_close_socket_fd(s);
1324 service_connection_unref(s);
1327 /* For the inactive states unit_notify() will trim the cgroup,
1328 * but for exit we have to do that ourselves... */
1329 if (state == SERVICE_EXITED)
1330 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1332 if (old_state != state)
1333 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1335 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1338 static int service_coldplug(Unit *u) {
1339 Service *s = SERVICE(u);
1343 assert(s->state == SERVICE_DEAD);
1345 if (s->deserialized_state != s->state) {
1347 if (s->deserialized_state == SERVICE_START_PRE ||
1348 s->deserialized_state == SERVICE_START ||
1349 s->deserialized_state == SERVICE_START_POST ||
1350 s->deserialized_state == SERVICE_RELOAD ||
1351 s->deserialized_state == SERVICE_STOP ||
1352 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1353 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1354 s->deserialized_state == SERVICE_STOP_POST ||
1355 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1356 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1357 s->deserialized_state == SERVICE_AUTO_RESTART) {
1359 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1362 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1364 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1369 if ((s->deserialized_state == SERVICE_START &&
1370 (s->type == SERVICE_FORKING ||
1371 s->type == SERVICE_DBUS ||
1372 s->type == SERVICE_ONESHOT ||
1373 s->type == SERVICE_NOTIFY)) ||
1374 s->deserialized_state == SERVICE_START_POST ||
1375 s->deserialized_state == SERVICE_RUNNING ||
1376 s->deserialized_state == SERVICE_RELOAD ||
1377 s->deserialized_state == SERVICE_STOP ||
1378 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1379 s->deserialized_state == SERVICE_STOP_SIGKILL)
1380 if (s->main_pid > 0)
1381 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1384 if (s->deserialized_state == SERVICE_START_PRE ||
1385 s->deserialized_state == SERVICE_START ||
1386 s->deserialized_state == SERVICE_START_POST ||
1387 s->deserialized_state == SERVICE_RELOAD ||
1388 s->deserialized_state == SERVICE_STOP ||
1389 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1390 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1391 s->deserialized_state == SERVICE_STOP_POST ||
1392 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1393 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1394 if (s->control_pid > 0)
1395 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1398 service_set_state(s, s->deserialized_state);
1404 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1408 unsigned rn_fds = 0;
1409 Set *set, *free_set = NULL;
1416 if (s->socket_fd >= 0)
1419 if (!set_isempty(s->configured_sockets))
1420 set = s->configured_sockets;
1422 if ((r = service_get_sockets(s, &free_set)) < 0)
1428 SET_FOREACH(sock, set, i) {
1432 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1444 if (!(t = new(int, rn_fds+cn_fds))) {
1450 memcpy(t, rfds, rn_fds);
1451 memcpy(t+rn_fds, cfds, cn_fds);
1456 rn_fds = rn_fds+cn_fds;
1474 static int service_spawn(
1479 bool apply_permissions,
1481 bool apply_tty_stdin,
1482 bool set_notify_socket,
1487 int *fds = NULL, *fdsbuf = NULL;
1488 unsigned n_fds = 0, n_env = 0;
1489 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1496 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1497 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1498 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1500 if (s->socket_fd >= 0) {
1501 fds = &s->socket_fd;
1504 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1511 if (timeout && s->timeout_usec) {
1512 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1515 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1517 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1522 if (!(our_env = new0(char*, 3))) {
1527 if (set_notify_socket)
1528 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1533 if (s->main_pid > 0)
1534 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1539 if (!(final_env = strv_env_merge(2,
1540 s->meta.manager->environment,
1555 s->meta.manager->confirm_spawn,
1556 s->meta.cgroup_bondings,
1563 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1564 /* FIXME: we need to do something here */
1570 strv_free(final_env);
1580 strv_free(final_env);
1583 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1588 static int main_pid_good(Service *s) {
1591 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1594 /* If we know the pid file, then lets just check if it is
1596 if (s->main_pid_known)
1597 return s->main_pid > 0;
1599 /* We don't know the pid */
1603 static int control_pid_good(Service *s) {
1606 return s->control_pid > 0;
1609 static int cgroup_good(Service *s) {
1614 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1620 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1627 if (allow_restart &&
1628 !s->forbid_restart &&
1629 (s->restart == SERVICE_RESTART_ALWAYS ||
1630 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure) ||
1631 (s->restart == SERVICE_RESTART_ON_FAILURE && s->failure) ||
1632 (s->restart == SERVICE_RESTART_ON_ABORT && s->failure &&
1633 (s->main_exec_status.code == CLD_KILLED ||
1634 s->main_exec_status.code == CLD_DUMPED)))) {
1636 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1639 service_set_state(s, SERVICE_AUTO_RESTART);
1641 service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD);
1643 s->forbid_restart = false;
1648 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1649 service_enter_dead(s, false, false);
1652 static void service_enter_signal(Service *s, ServiceState state, bool success);
1654 static void service_enter_stop_post(Service *s, bool success) {
1661 service_unwatch_control_pid(s);
1663 s->control_command_id = SERVICE_EXEC_STOP_POST;
1664 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1665 if ((r = service_spawn(s,
1669 !s->permissions_start_only,
1670 !s->root_directory_start_only,
1673 &s->control_pid)) < 0)
1677 service_set_state(s, SERVICE_STOP_POST);
1679 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1684 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1685 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1688 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1690 Set *pid_set = NULL;
1691 bool wait_for_exit = false;
1698 if (s->exec_context.kill_mode != KILL_NONE) {
1699 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1701 if (s->main_pid > 0) {
1702 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1704 s->main_pid, sig) < 0 && errno != ESRCH)
1706 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1708 wait_for_exit = true;
1711 if (s->control_pid > 0) {
1712 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1714 s->control_pid, sig) < 0 && errno != ESRCH)
1716 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1718 wait_for_exit = true;
1721 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1723 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1728 /* Exclude the main/control pids from being killed via the cgroup */
1729 if (s->main_pid > 0)
1730 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1733 if (s->control_pid > 0)
1734 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1737 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
1738 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1739 log_warning("Failed to kill control group: %s", strerror(-r));
1741 wait_for_exit = true;
1747 if (wait_for_exit) {
1748 if (s->timeout_usec > 0)
1749 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1752 service_set_state(s, state);
1753 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1754 service_enter_stop_post(s, true);
1756 service_enter_dead(s, true, true);
1761 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1763 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1764 service_enter_stop_post(s, false);
1766 service_enter_dead(s, false, true);
1772 static void service_enter_stop(Service *s, bool success) {
1780 service_unwatch_control_pid(s);
1782 s->control_command_id = SERVICE_EXEC_STOP;
1783 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1784 if ((r = service_spawn(s,
1788 !s->permissions_start_only,
1789 !s->root_directory_start_only,
1792 &s->control_pid)) < 0)
1795 service_set_state(s, SERVICE_STOP);
1797 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1802 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1803 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1806 static void service_enter_running(Service *s, bool success) {
1807 int main_pid_ok, cgroup_ok;
1813 main_pid_ok = main_pid_good(s);
1814 cgroup_ok = cgroup_good(s);
1816 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1817 (s->bus_name_good || s->type != SERVICE_DBUS))
1818 service_set_state(s, SERVICE_RUNNING);
1819 else if (s->remain_after_exit)
1820 service_set_state(s, SERVICE_EXITED);
1822 service_enter_stop(s, true);
1825 static void service_enter_start_post(Service *s) {
1829 service_unwatch_control_pid(s);
1831 s->control_command_id = SERVICE_EXEC_START_POST;
1832 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1833 if ((r = service_spawn(s,
1837 !s->permissions_start_only,
1838 !s->root_directory_start_only,
1841 &s->control_pid)) < 0)
1844 service_set_state(s, SERVICE_START_POST);
1846 service_enter_running(s, true);
1851 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1852 service_enter_stop(s, false);
1855 static void service_enter_start(Service *s) {
1861 assert(s->exec_command[SERVICE_EXEC_START]);
1862 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1864 if (s->type == SERVICE_FORKING)
1865 service_unwatch_control_pid(s);
1867 service_unwatch_main_pid(s);
1869 s->control_command_id = SERVICE_EXEC_START;
1870 s->control_command = s->exec_command[SERVICE_EXEC_START];
1872 if ((r = service_spawn(s,
1874 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1879 s->notify_access != NOTIFY_NONE,
1883 if (s->type == SERVICE_SIMPLE) {
1884 /* For simple services we immediately start
1885 * the START_POST binaries. */
1887 service_set_main_pid(s, pid);
1888 service_enter_start_post(s);
1890 } else if (s->type == SERVICE_FORKING) {
1892 /* For forking services we wait until the start
1893 * process exited. */
1895 s->control_pid = pid;
1896 service_set_state(s, SERVICE_START);
1898 } else if (s->type == SERVICE_ONESHOT ||
1899 s->type == SERVICE_DBUS ||
1900 s->type == SERVICE_NOTIFY) {
1902 /* For oneshot services we wait until the start
1903 * process exited, too, but it is our main process. */
1905 /* For D-Bus services we know the main pid right away,
1906 * but wait for the bus name to appear on the
1907 * bus. Notify services are similar. */
1909 service_set_main_pid(s, pid);
1910 service_set_state(s, SERVICE_START);
1912 assert_not_reached("Unknown service type");
1917 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1918 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1921 static void service_enter_start_pre(Service *s) {
1926 service_unwatch_control_pid(s);
1928 s->control_command_id = SERVICE_EXEC_START_PRE;
1929 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1930 if ((r = service_spawn(s,
1934 !s->permissions_start_only,
1935 !s->root_directory_start_only,
1938 &s->control_pid)) < 0)
1941 service_set_state(s, SERVICE_START_PRE);
1943 service_enter_start(s);
1948 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1949 service_enter_dead(s, false, true);
1952 static void service_enter_restart(Service *s) {
1957 dbus_error_init(&error);
1959 service_enter_dead(s, true, false);
1961 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
1964 log_debug("%s scheduled restart job.", s->meta.id);
1968 log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
1969 service_enter_dead(s, false, false);
1971 dbus_error_free(&error);
1974 static void service_enter_reload(Service *s) {
1979 service_unwatch_control_pid(s);
1981 s->control_command_id = SERVICE_EXEC_RELOAD;
1982 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1983 if ((r = service_spawn(s,
1987 !s->permissions_start_only,
1988 !s->root_directory_start_only,
1991 &s->control_pid)) < 0)
1994 service_set_state(s, SERVICE_RELOAD);
1996 service_enter_running(s, true);
2001 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
2002 service_enter_stop(s, false);
2005 static void service_run_next_control(Service *s, bool success) {
2009 assert(s->control_command);
2010 assert(s->control_command->command_next);
2015 assert(s->control_command_id != SERVICE_EXEC_START);
2017 s->control_command = s->control_command->command_next;
2018 service_unwatch_control_pid(s);
2020 if ((r = service_spawn(s,
2024 !s->permissions_start_only,
2025 !s->root_directory_start_only,
2026 s->control_command_id == SERVICE_EXEC_START_PRE ||
2027 s->control_command_id == SERVICE_EXEC_STOP_POST,
2029 &s->control_pid)) < 0)
2035 log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
2037 if (s->state == SERVICE_START_PRE)
2038 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2039 else if (s->state == SERVICE_STOP)
2040 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2041 else if (s->state == SERVICE_STOP_POST)
2042 service_enter_dead(s, false, true);
2044 service_enter_stop(s, false);
2047 static void service_run_next_main(Service *s, bool success) {
2052 assert(s->control_command);
2053 assert(s->control_command->command_next);
2058 assert(s->control_command_id == SERVICE_EXEC_START);
2059 assert(s->type == SERVICE_ONESHOT);
2061 s->control_command = s->control_command->command_next;
2062 service_unwatch_main_pid(s);
2064 if ((r = service_spawn(s,
2071 s->notify_access != NOTIFY_NONE,
2075 service_set_main_pid(s, pid);
2080 log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
2081 service_enter_stop(s, false);
2084 static int service_start(Unit *u) {
2085 Service *s = SERVICE(u);
2089 /* We cannot fulfill this request right now, try again later
2091 if (s->state == SERVICE_STOP ||
2092 s->state == SERVICE_STOP_SIGTERM ||
2093 s->state == SERVICE_STOP_SIGKILL ||
2094 s->state == SERVICE_STOP_POST ||
2095 s->state == SERVICE_FINAL_SIGTERM ||
2096 s->state == SERVICE_FINAL_SIGKILL)
2099 /* Already on it! */
2100 if (s->state == SERVICE_START_PRE ||
2101 s->state == SERVICE_START ||
2102 s->state == SERVICE_START_POST)
2105 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
2107 /* Make sure we don't enter a busy loop of some kind. */
2108 if (!ratelimit_test(&s->ratelimit)) {
2109 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
2114 s->main_pid_known = false;
2115 s->forbid_restart = false;
2117 service_enter_start_pre(s);
2121 static int service_stop(Unit *u) {
2122 Service *s = SERVICE(u);
2126 /* This is a user request, so don't do restarts on this
2128 s->forbid_restart = true;
2131 if (s->state == SERVICE_STOP ||
2132 s->state == SERVICE_STOP_SIGTERM ||
2133 s->state == SERVICE_STOP_SIGKILL ||
2134 s->state == SERVICE_STOP_POST ||
2135 s->state == SERVICE_FINAL_SIGTERM ||
2136 s->state == SERVICE_FINAL_SIGKILL)
2139 /* Don't allow a restart */
2140 if (s->state == SERVICE_AUTO_RESTART) {
2141 service_set_state(s, SERVICE_DEAD);
2145 /* If there's already something running we go directly into
2147 if (s->state == SERVICE_START_PRE ||
2148 s->state == SERVICE_START ||
2149 s->state == SERVICE_START_POST ||
2150 s->state == SERVICE_RELOAD) {
2151 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2155 assert(s->state == SERVICE_RUNNING ||
2156 s->state == SERVICE_EXITED);
2158 service_enter_stop(s, true);
2162 static int service_reload(Unit *u) {
2163 Service *s = SERVICE(u);
2167 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2169 service_enter_reload(s);
2173 static bool service_can_reload(Unit *u) {
2174 Service *s = SERVICE(u);
2178 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2181 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2182 Service *s = SERVICE(u);
2188 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2189 unit_serialize_item(u, f, "failure", yes_no(s->failure));
2191 if (s->control_pid > 0)
2192 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2194 if (s->main_pid_known && s->main_pid > 0)
2195 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2197 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2200 unit_serialize_item(u, f, "status-text", s->status_text);
2202 /* There's a minor uncleanliness here: if there are multiple
2203 * commands attached here, we will start from the first one
2205 if (s->control_command_id >= 0)
2206 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2208 if (s->socket_fd >= 0) {
2211 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2214 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2217 if (s->main_exec_status.pid > 0) {
2218 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2220 if (s->main_exec_status.start_timestamp.realtime > 0) {
2221 unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2222 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2224 unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2225 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2228 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2229 unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2230 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2231 unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2232 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2234 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2235 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2242 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2243 Service *s = SERVICE(u);
2250 if (streq(key, "state")) {
2253 if ((state = service_state_from_string(value)) < 0)
2254 log_debug("Failed to parse state value %s", value);
2256 s->deserialized_state = state;
2257 } else if (streq(key, "failure")) {
2260 if ((b = parse_boolean(value)) < 0)
2261 log_debug("Failed to parse failure value %s", value);
2263 s->failure = b || s->failure;
2264 } else if (streq(key, "control-pid")) {
2267 if (parse_pid(value, &pid) < 0)
2268 log_debug("Failed to parse control-pid value %s", value);
2270 s->control_pid = pid;
2271 } else if (streq(key, "main-pid")) {
2274 if (parse_pid(value, &pid) < 0)
2275 log_debug("Failed to parse main-pid value %s", value);
2277 service_set_main_pid(s, (pid_t) pid);
2278 } else if (streq(key, "main-pid-known")) {
2281 if ((b = parse_boolean(value)) < 0)
2282 log_debug("Failed to parse main-pid-known value %s", value);
2284 s->main_pid_known = b;
2285 } else if (streq(key, "status-text")) {
2288 if ((t = strdup(value))) {
2289 free(s->status_text);
2293 } else if (streq(key, "control-command")) {
2294 ServiceExecCommand id;
2296 if ((id = service_exec_command_from_string(value)) < 0)
2297 log_debug("Failed to parse exec-command value %s", value);
2299 s->control_command_id = id;
2300 s->control_command = s->exec_command[id];
2302 } else if (streq(key, "socket-fd")) {
2305 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2306 log_debug("Failed to parse socket-fd value %s", value);
2309 if (s->socket_fd >= 0)
2310 close_nointr_nofail(s->socket_fd);
2311 s->socket_fd = fdset_remove(fds, fd);
2313 } else if (streq(key, "main-exec-status-pid")) {
2316 if (parse_pid(value, &pid) < 0)
2317 log_debug("Failed to parse main-exec-status-pid value %s", value);
2319 s->main_exec_status.pid = pid;
2320 } else if (streq(key, "main-exec-status-code")) {
2323 if (safe_atoi(value, &i) < 0)
2324 log_debug("Failed to parse main-exec-status-code value %s", value);
2326 s->main_exec_status.code = i;
2327 } else if (streq(key, "main-exec-status-status")) {
2330 if (safe_atoi(value, &i) < 0)
2331 log_debug("Failed to parse main-exec-status-status value %s", value);
2333 s->main_exec_status.status = i;
2334 } else if (streq(key, "main-exec-status-start-realtime")) {
2337 if (safe_atou64(value, &k) < 0)
2338 log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2340 s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2341 } else if (streq(key, "main-exec-status-start-monotonic")) {
2344 if (safe_atou64(value, &k) < 0)
2345 log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2347 s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2348 } else if (streq(key, "main-exec-status-exit-realtime")) {
2351 if (safe_atou64(value, &k) < 0)
2352 log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2354 s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2355 } else if (streq(key, "main-exec-status-exit-monotonic")) {
2358 if (safe_atou64(value, &k) < 0)
2359 log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2361 s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2363 log_debug("Unknown serialization key '%s'", key);
2368 static UnitActiveState service_active_state(Unit *u) {
2371 return state_translation_table[SERVICE(u)->state];
2374 static const char *service_sub_state_to_string(Unit *u) {
2377 return service_state_to_string(SERVICE(u)->state);
2380 #ifdef HAVE_SYSV_COMPAT
2381 static bool service_check_gc(Unit *u) {
2382 Service *s = SERVICE(u);
2386 return !!s->sysv_path;
2390 static bool service_check_snapshot(Unit *u) {
2391 Service *s = SERVICE(u);
2395 return !s->got_socket_fd;
2398 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2399 Service *s = SERVICE(u);
2405 if (!s->meta.fragment_path)
2406 success = is_clean_exit_lsb(code, status);
2408 success = is_clean_exit(code, status);
2410 if (s->main_pid == pid) {
2413 exec_status_exit(&s->main_exec_status, pid, code, status, s->exec_context.utmp_id);
2415 if (s->type != SERVICE_FORKING && s->control_command) {
2416 s->control_command->exec_status = s->main_exec_status;
2418 if (s->control_command->ignore)
2422 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2423 "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2424 s->failure = s->failure || !success;
2426 if (s->control_command &&
2427 s->control_command->command_next &&
2430 /* There is another command to *
2431 * execute, so let's do that. */
2433 log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2434 service_run_next_main(s, success);
2438 /* The service exited, so the service is officially
2441 s->control_command = NULL;
2442 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2446 case SERVICE_START_POST:
2447 case SERVICE_RELOAD:
2449 /* Need to wait until the operation is
2454 if (s->type == SERVICE_ONESHOT) {
2455 /* This was our main goal, so let's go on */
2457 service_enter_start_post(s);
2459 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2462 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2467 case SERVICE_RUNNING:
2468 service_enter_running(s, success);
2471 case SERVICE_STOP_SIGTERM:
2472 case SERVICE_STOP_SIGKILL:
2474 if (!control_pid_good(s))
2475 service_enter_stop_post(s, success);
2477 /* If there is still a control process, wait for that first */
2481 assert_not_reached("Uh, main process died at wrong time.");
2485 } else if (s->control_pid == pid) {
2489 if (s->control_command) {
2490 exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id);
2492 if (s->control_command->ignore)
2496 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2497 "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2498 s->failure = s->failure || !success;
2500 if (s->control_command &&
2501 s->control_command->command_next &&
2504 /* There is another command to *
2505 * execute, so let's do that. */
2507 log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2508 service_run_next_control(s, success);
2511 /* No further commands for this step, so let's
2512 * figure out what to do next */
2514 s->control_command = NULL;
2515 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2517 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2521 case SERVICE_START_PRE:
2523 service_enter_start(s);
2525 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2529 assert(s->type == SERVICE_FORKING);
2531 /* Let's try to load the pid
2532 * file here if we can. We
2533 * ignore the return value,
2534 * since the PID file might
2535 * actually be created by a
2536 * START_POST script */
2540 service_load_pid_file(s);
2542 service_enter_start_post(s);
2544 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2548 case SERVICE_START_POST:
2549 if (success && s->pid_file && !s->main_pid_known) {
2552 /* Hmm, let's see if we can
2553 * load the pid now after the
2554 * start-post scripts got
2557 if ((r = service_load_pid_file(s)) < 0)
2558 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2563 case SERVICE_RELOAD:
2565 service_enter_running(s, true);
2567 service_enter_stop(s, false);
2572 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2575 case SERVICE_STOP_SIGTERM:
2576 case SERVICE_STOP_SIGKILL:
2577 if (main_pid_good(s) <= 0)
2578 service_enter_stop_post(s, success);
2580 /* If there is still a service
2581 * process around, wait until
2582 * that one quit, too */
2585 case SERVICE_STOP_POST:
2586 case SERVICE_FINAL_SIGTERM:
2587 case SERVICE_FINAL_SIGKILL:
2588 service_enter_dead(s, success, true);
2592 assert_not_reached("Uh, control process died at wrong time.");
2597 /* Notify clients about changed exit status */
2598 unit_add_to_dbus_queue(u);
2601 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2602 Service *s = SERVICE(u);
2605 assert(elapsed == 1);
2607 assert(w == &s->timer_watch);
2611 case SERVICE_START_PRE:
2613 log_warning("%s operation timed out. Terminating.", u->meta.id);
2614 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2617 case SERVICE_START_POST:
2618 case SERVICE_RELOAD:
2619 log_warning("%s operation timed out. Stopping.", u->meta.id);
2620 service_enter_stop(s, false);
2624 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2625 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2628 case SERVICE_STOP_SIGTERM:
2629 log_warning("%s stopping timed out. Killing.", u->meta.id);
2630 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2633 case SERVICE_STOP_SIGKILL:
2634 /* Uh, wie sent a SIGKILL and it is still not gone?
2635 * Must be something we cannot kill, so let's just be
2636 * weirded out and continue */
2638 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2639 service_enter_stop_post(s, false);
2642 case SERVICE_STOP_POST:
2643 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2644 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2647 case SERVICE_FINAL_SIGTERM:
2648 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2649 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2652 case SERVICE_FINAL_SIGKILL:
2653 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
2654 service_enter_dead(s, false, true);
2657 case SERVICE_AUTO_RESTART:
2658 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2659 service_enter_restart(s);
2663 assert_not_reached("Timeout at wrong time.");
2667 static void service_cgroup_notify_event(Unit *u) {
2668 Service *s = SERVICE(u);
2672 log_debug("%s: cgroup is empty", u->meta.id);
2676 /* Waiting for SIGCHLD is usually more interesting,
2677 * because it includes return codes/signals. Which is
2678 * why we ignore the cgroup events for most cases,
2679 * except when we don't know pid which to expect the
2682 case SERVICE_RUNNING:
2683 service_enter_running(s, true);
2686 case SERVICE_STOP_SIGTERM:
2687 case SERVICE_STOP_SIGKILL:
2688 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2689 service_enter_stop_post(s, true);
2693 case SERVICE_FINAL_SIGTERM:
2694 case SERVICE_FINAL_SIGKILL:
2695 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2696 service_enter_dead(s, true, true);
2705 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2706 Service *s = SERVICE(u);
2711 if (s->notify_access == NOTIFY_NONE) {
2712 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2713 u->meta.id, (unsigned long) pid);
2717 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2718 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2719 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2723 log_debug("%s: Got message", u->meta.id);
2725 /* Interpret MAINPID= */
2726 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2727 (s->state == SERVICE_START ||
2728 s->state == SERVICE_START_POST ||
2729 s->state == SERVICE_RUNNING ||
2730 s->state == SERVICE_RELOAD)) {
2732 if (parse_pid(e + 8, &pid) < 0)
2733 log_warning("Failed to parse notification message %s", e);
2735 log_debug("%s: got %s", u->meta.id, e);
2736 service_set_main_pid(s, pid);
2740 /* Interpret READY= */
2741 if (s->type == SERVICE_NOTIFY &&
2742 s->state == SERVICE_START &&
2743 strv_find(tags, "READY=1")) {
2744 log_debug("%s: got READY=1", u->meta.id);
2746 service_enter_start_post(s);
2749 /* Interpret STATUS= */
2750 if ((e = strv_find_prefix(tags, "STATUS="))) {
2754 if (!(t = strdup(e+7))) {
2755 log_error("Failed to allocate string.");
2759 log_debug("%s: got %s", u->meta.id, e);
2761 free(s->status_text);
2764 free(s->status_text);
2765 s->status_text = NULL;
2770 /* Notify clients about changed status or main pid */
2771 unit_add_to_dbus_queue(u);
2774 #ifdef HAVE_SYSV_COMPAT
2775 static int service_enumerate(Manager *m) {
2779 char *path = NULL, *fpath = NULL, *name = NULL;
2780 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
2785 Unit *previous = NULL;
2786 char *arch_daemons = NULL;
2787 char *arch_daemons_stripped = NULL;
2788 char **arch_daemons_split = NULL;
2794 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
2795 "DAEMONS", &arch_daemons,
2799 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
2801 } else if (arch_daemons) {
2802 if (!(arch_daemons_stripped = strchr(arch_daemons, '(')))
2803 arch_daemons_stripped = arch_daemons;
2805 arch_daemons_stripped++; /* strip start paren */
2807 arch_daemons_stripped[strcspn(arch_daemons_stripped, ")")] = 0; /* strip end paren */
2809 if (!(arch_daemons_split = strv_split_quoted(arch_daemons_stripped))) {
2814 STRV_FOREACH(p, arch_daemons_split) {
2819 if (**p == '!') /* daemons prefixed with ! are disabled, so ignore them */
2822 if (!(name = sysv_translate_name(*p))) {
2827 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2828 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2832 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, "multi-user.target", NULL, true)) < 0)
2836 if ((r = unit_add_dependency(service, UNIT_AFTER, previous, true)) < 0)
2839 if (**p != '@') /* daemons prefixed with @ can be started in the background */
2845 zero(runlevel_services);
2847 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2848 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2853 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2861 if (!(d = opendir(path))) {
2862 if (errno != ENOENT)
2863 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2868 while ((de = readdir(d))) {
2871 if (ignore_file(de->d_name))
2874 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2877 if (strlen(de->d_name) < 4)
2880 a = undecchar(de->d_name[1]);
2881 b = undecchar(de->d_name[2]);
2888 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2893 if (access(fpath, X_OK) < 0) {
2895 if (errno != ENOENT)
2896 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2902 if (!(name = sysv_translate_name(de->d_name + 3))) {
2907 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2908 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2912 if (de->d_name[0] == 'S') {
2914 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
2915 SERVICE(service)->sysv_start_priority =
2916 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2918 SERVICE(service)->sysv_enabled = true;
2921 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
2924 if ((r = set_put(runlevel_services[i], service)) < 0)
2927 } else if (de->d_name[0] == 'K' &&
2928 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2929 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2931 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
2934 if ((r = set_put(shutdown_services, service)) < 0)
2940 /* Now we loaded all stubs and are aware of the lowest
2941 start-up priority for all services, not let's actually load
2942 the services, this will also tell us which services are
2943 actually native now */
2944 manager_dispatch_load_queue(m);
2946 /* If this is a native service, rely on native ways to pull in
2947 * a service, don't pull it in via sysv rcN.d links. */
2948 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
2949 SET_FOREACH(service, runlevel_services[i], j) {
2950 service = unit_follow_merge(service);
2952 if (service->meta.fragment_path)
2955 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2959 /* We honour K links only for halt/reboot. For the normal
2960 * runlevels we assume the stop jobs will be implicitly added
2961 * by the core logic. Also, we don't really distuingish here
2962 * between the runlevels 0 and 6 and just add them to the
2963 * special shutdown target. On SUSE the boot.d/ runlevel is
2964 * also used for shutdown, so we add links for that too to the
2965 * shutdown target.*/
2966 SET_FOREACH(service, shutdown_services, j) {
2967 service = unit_follow_merge(service);
2969 if (service->meta.fragment_path)
2972 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2984 free(arch_daemons_split);
2987 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
2988 set_free(runlevel_services[i]);
2989 set_free(shutdown_services);
2998 static void service_bus_name_owner_change(
3001 const char *old_owner,
3002 const char *new_owner) {
3004 Service *s = SERVICE(u);
3009 assert(streq(s->bus_name, name));
3010 assert(old_owner || new_owner);
3012 if (old_owner && new_owner)
3013 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
3015 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
3017 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
3019 s->bus_name_good = !!new_owner;
3021 if (s->type == SERVICE_DBUS) {
3023 /* service_enter_running() will figure out what to
3025 if (s->state == SERVICE_RUNNING)
3026 service_enter_running(s, true);
3027 else if (s->state == SERVICE_START && new_owner)
3028 service_enter_start_post(s);
3030 } else if (new_owner &&
3032 (s->state == SERVICE_START ||
3033 s->state == SERVICE_START_POST ||
3034 s->state == SERVICE_RUNNING ||
3035 s->state == SERVICE_RELOAD)) {
3037 /* Try to acquire PID from bus service */
3038 log_debug("Trying to acquire PID from D-Bus name...");
3040 bus_query_pid(u->meta.manager, name);
3044 static void service_bus_query_pid_done(
3049 Service *s = SERVICE(u);
3054 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
3056 if (s->main_pid <= 0 &&
3057 (s->state == SERVICE_START ||
3058 s->state == SERVICE_START_POST ||
3059 s->state == SERVICE_RUNNING ||
3060 s->state == SERVICE_RELOAD))
3061 service_set_main_pid(s, pid);
3064 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3068 /* This is called by the socket code when instantiating a new
3069 * service for a stream socket and the socket needs to be
3072 if (s->meta.load_state != UNIT_LOADED)
3075 if (s->socket_fd >= 0)
3078 if (s->state != SERVICE_DEAD)
3082 s->got_socket_fd = true;
3083 s->accept_socket = sock;
3088 static void service_reset_failed(Unit *u) {
3089 Service *s = SERVICE(u);
3093 if (s->state == SERVICE_FAILED)
3094 service_set_state(s, SERVICE_DEAD);
3099 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3100 [SERVICE_DEAD] = "dead",
3101 [SERVICE_START_PRE] = "start-pre",
3102 [SERVICE_START] = "start",
3103 [SERVICE_START_POST] = "start-post",
3104 [SERVICE_RUNNING] = "running",
3105 [SERVICE_EXITED] = "exited",
3106 [SERVICE_RELOAD] = "reload",
3107 [SERVICE_STOP] = "stop",
3108 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3109 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3110 [SERVICE_STOP_POST] = "stop-post",
3111 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3112 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3113 [SERVICE_FAILED] = "failed",
3114 [SERVICE_AUTO_RESTART] = "auto-restart",
3117 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3119 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3120 [SERVICE_RESTART_NO] = "no",
3121 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3122 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3123 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3124 [SERVICE_RESTART_ALWAYS] = "always"
3127 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3129 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3130 [SERVICE_SIMPLE] = "simple",
3131 [SERVICE_FORKING] = "forking",
3132 [SERVICE_ONESHOT] = "oneshot",
3133 [SERVICE_DBUS] = "dbus",
3134 [SERVICE_NOTIFY] = "notify"
3137 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3139 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3140 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3141 [SERVICE_EXEC_START] = "ExecStart",
3142 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3143 [SERVICE_EXEC_RELOAD] = "ExecReload",
3144 [SERVICE_EXEC_STOP] = "ExecStop",
3145 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3148 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3150 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3151 [NOTIFY_NONE] = "none",
3152 [NOTIFY_MAIN] = "main",
3153 [NOTIFY_ALL] = "all"
3156 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3158 const UnitVTable service_vtable = {
3159 .suffix = ".service",
3160 .show_status = true,
3162 .init = service_init,
3163 .done = service_done,
3164 .load = service_load,
3166 .coldplug = service_coldplug,
3168 .dump = service_dump,
3170 .start = service_start,
3171 .stop = service_stop,
3172 .reload = service_reload,
3174 .can_reload = service_can_reload,
3176 .serialize = service_serialize,
3177 .deserialize_item = service_deserialize_item,
3179 .active_state = service_active_state,
3180 .sub_state_to_string = service_sub_state_to_string,
3182 #ifdef HAVE_SYSV_COMPAT
3183 .check_gc = service_check_gc,
3185 .check_snapshot = service_check_snapshot,
3187 .sigchld_event = service_sigchld_event,
3188 .timer_event = service_timer_event,
3190 .reset_failed = service_reset_failed,
3192 .cgroup_notify_empty = service_cgroup_notify_event,
3193 .notify_message = service_notify_message,
3195 .bus_name_owner_change = service_bus_name_owner_change,
3196 .bus_query_pid_done = service_bus_query_pid_done,
3198 .bus_interface = "org.freedesktop.systemd1.Service",
3199 .bus_message_handler = bus_service_message_handler,
3200 .bus_invalidating_properties = bus_service_invalidating_properties,
3202 #ifdef HAVE_SYSV_COMPAT
3203 .enumerate = service_enumerate