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"
37 #include "exit-status.h"
39 #define COMMENTS "#;\n"
40 #define NEWLINES "\n\r"
42 #ifdef HAVE_SYSV_COMPAT
44 #define DEFAULT_SYSV_TIMEOUT_USEC (3*USEC_PER_MINUTE)
46 typedef enum RunlevelType {
55 const RunlevelType type;
57 /* Standard SysV runlevels for start-up */
58 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
59 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
60 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
61 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
62 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
65 /* SUSE style boot.d */
66 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
69 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_FRUGALWARE)
70 /* Debian style rcS.d */
71 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
74 /* Standard SysV runlevels for shutdown */
75 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
76 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
78 /* Note that the order here matters, as we read the
79 directories in this order, and we want to make sure that
80 sysv_start_priority is known when we first load the
81 unit. And that value we only know from S links. Hence
82 UP/SYSINIT must be read before DOWN */
85 #define RUNLEVELS_UP "12345"
86 /* #define RUNLEVELS_DOWN "06" */
87 /* #define RUNLEVELS_BOOT "bBsS" */
90 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
91 [SERVICE_DEAD] = UNIT_INACTIVE,
92 [SERVICE_START_PRE] = UNIT_ACTIVATING,
93 [SERVICE_START] = UNIT_ACTIVATING,
94 [SERVICE_START_POST] = UNIT_ACTIVATING,
95 [SERVICE_RUNNING] = UNIT_ACTIVE,
96 [SERVICE_EXITED] = UNIT_ACTIVE,
97 [SERVICE_RELOAD] = UNIT_RELOADING,
98 [SERVICE_STOP] = UNIT_DEACTIVATING,
99 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
100 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
101 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
102 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
103 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
104 [SERVICE_FAILED] = UNIT_FAILED,
105 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
108 static void service_init(Unit *u) {
109 Service *s = SERVICE(u);
112 assert(u->meta.load_state == UNIT_STUB);
114 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
115 s->restart_usec = DEFAULT_RESTART_USEC;
116 s->timer_watch.type = WATCH_INVALID;
117 #ifdef HAVE_SYSV_COMPAT
118 s->sysv_start_priority = -1;
122 exec_context_init(&s->exec_context);
124 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
126 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
129 static void service_unwatch_control_pid(Service *s) {
132 if (s->control_pid <= 0)
135 unit_unwatch_pid(UNIT(s), s->control_pid);
139 static void service_unwatch_main_pid(Service *s) {
142 if (s->main_pid <= 0)
145 unit_unwatch_pid(UNIT(s), s->main_pid);
149 static int service_set_main_pid(Service *s, pid_t pid) {
160 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
161 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
162 s->meta.id, (unsigned long) pid);
165 s->main_pid_known = true;
167 exec_status_start(&s->main_exec_status, pid);
172 static void service_close_socket_fd(Service *s) {
175 if (s->socket_fd < 0)
178 close_nointr_nofail(s->socket_fd);
182 static void service_connection_unref(Service *s) {
185 if (!s->accept_socket)
188 socket_connection_unref(s->accept_socket);
189 s->accept_socket = NULL;
192 static void service_done(Unit *u) {
193 Service *s = SERVICE(u);
200 #ifdef HAVE_SYSV_COMPAT
204 free(s->sysv_runlevels);
205 s->sysv_runlevels = NULL;
208 free(s->status_text);
209 s->status_text = NULL;
211 exec_context_done(&s->exec_context);
212 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
213 s->control_command = NULL;
215 /* This will leak a process, but at least no memory or any of
217 service_unwatch_main_pid(s);
218 service_unwatch_control_pid(s);
221 unit_unwatch_bus_name(UNIT(u), s->bus_name);
226 service_close_socket_fd(s);
227 service_connection_unref(s);
229 set_free(s->configured_sockets);
231 unit_unwatch_timer(u, &s->timer_watch);
234 #ifdef HAVE_SYSV_COMPAT
235 static char *sysv_translate_name(const char *name) {
238 if (!(r = new(char, strlen(name) + sizeof(".service"))))
241 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
242 if (endswith(name, ".sh"))
243 /* Drop Debian-style .sh suffix */
244 strcpy(stpcpy(r, name) - 3, ".service");
247 if (startswith(name, "boot."))
248 /* Drop SuSE-style boot. prefix */
249 strcpy(stpcpy(r, name + 5), ".service");
251 #ifdef TARGET_FRUGALWARE
252 if (startswith(name, "rc."))
253 /* Drop Frugalware-style rc. prefix */
254 strcpy(stpcpy(r, name + 3), ".service");
257 /* Normal init scripts */
258 strcpy(stpcpy(r, name), ".service");
263 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
265 /* We silently ignore the $ prefix here. According to the LSB
266 * spec it simply indicates whether something is a
267 * standardized name or a distribution-specific one. Since we
268 * just follow what already exists and do not introduce new
269 * uses or names we don't care who introduced a new name. */
271 static const char * const table[] = {
272 /* LSB defined facilities */
273 "local_fs", SPECIAL_LOCAL_FS_TARGET,
274 "network", SPECIAL_NETWORK_TARGET,
275 "named", SPECIAL_NSS_LOOKUP_TARGET,
276 "portmap", SPECIAL_RPCBIND_TARGET,
277 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
278 "syslog", SPECIAL_SYSLOG_TARGET,
279 "time", SPECIAL_RTC_SET_TARGET,
281 /* common extensions */
282 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
283 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
286 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
287 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
291 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
292 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
293 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
297 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
308 n = *name == '$' ? name + 1 : name;
310 for (i = 0; i < ELEMENTSOF(table); i += 2) {
312 if (!streq(table[i], n))
318 if (!(r = strdup(table[i+1])))
324 /* If we don't know this name, fallback heuristics to figure
325 * out whether something is a target or a service alias. */
328 if (!unit_prefix_is_valid(n))
331 /* Facilities starting with $ are most likely targets */
332 r = unit_name_build(n, NULL, ".target");
333 } else if (filename && streq(name, filename))
334 /* Names equalling the file name of the services are redundant */
337 /* Everything else we assume to be normal service names */
338 r = sysv_translate_name(n);
351 static int sysv_fix_order(Service *s) {
357 if (s->sysv_start_priority < 0)
360 /* For each pair of services where at least one lacks a LSB
361 * header, we use the start priority value to order things. */
363 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
366 bool special_s, special_t;
368 t = (Service*) other;
373 if (t->meta.load_state != UNIT_LOADED)
376 if (t->sysv_start_priority < 0)
379 /* If both units have modern headers we don't care
380 * about the priorities */
381 if ((s->meta.fragment_path || s->sysv_has_lsb) &&
382 (t->meta.fragment_path || t->sysv_has_lsb))
385 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
386 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
388 if (special_t && !special_s)
390 else if (special_s && !special_t)
392 else if (t->sysv_start_priority < s->sysv_start_priority)
394 else if (t->sysv_start_priority > s->sysv_start_priority)
399 /* FIXME: Maybe we should compare the name here lexicographically? */
401 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
408 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
411 if (!(c = new0(ExecCommand, 1)))
414 if (!(c->path = strdup(path))) {
419 if (!(c->argv = strv_new(path, arg1, NULL))) {
428 static int sysv_exec_commands(Service *s) {
432 assert(s->sysv_path);
434 if (!(c = exec_command_new(s->sysv_path, "start")))
436 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
438 if (!(c = exec_command_new(s->sysv_path, "stop")))
440 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
442 if (!(c = exec_command_new(s->sysv_path, "reload")))
444 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
449 static int service_load_sysv_path(Service *s, const char *path) {
460 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
467 if (!(f = fopen(path, "re"))) {
468 r = errno == ENOENT ? 0 : -errno;
473 if (!(s->sysv_path = strdup(path))) {
479 char l[LINE_MAX], *t;
481 if (!fgets(l, sizeof(l), f)) {
486 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
496 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
498 s->sysv_has_lsb = true;
502 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
508 t += strspn(t, WHITESPACE);
510 if (state == NORMAL) {
512 /* Try to parse Red Hat style chkconfig headers */
514 if (startswith_no_case(t, "chkconfig:")) {
516 char runlevels[16], *k;
520 if (sscanf(t+10, "%15s %i %*i",
522 &start_priority) != 2) {
524 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
528 /* A start priority gathered from the
529 * symlink farms is preferred over the
530 * data from the LSB header. */
531 if (start_priority < 0 || start_priority > 99)
532 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
533 else if (s->sysv_start_priority < 0)
534 s->sysv_start_priority = start_priority;
536 char_array_0(runlevels);
537 k = delete_chars(runlevels, WHITESPACE "-");
542 if (!(d = strdup(k))) {
547 free(s->sysv_runlevels);
548 s->sysv_runlevels = d;
551 } else if (startswith_no_case(t, "description:")) {
553 size_t k = strlen(t);
557 if (t[k-1] == '\\') {
562 if ((j = strstrip(t+12)) && *j) {
563 if (!(d = strdup(j))) {
570 free(chkconfig_description);
571 chkconfig_description = d;
573 } else if (startswith_no_case(t, "pidfile:")) {
580 if (!path_is_absolute(fn)) {
581 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
585 if (!(fn = strdup(fn))) {
594 } else if (state == DESCRIPTION) {
596 /* Try to parse Red Hat style description
599 size_t k = strlen(t);
607 if ((j = strstrip(t)) && *j) {
610 if (chkconfig_description)
611 asprintf(&d, "%s %s", chkconfig_description, j);
620 free(chkconfig_description);
621 chkconfig_description = d;
624 } else if (state == LSB || state == LSB_DESCRIPTION) {
626 if (startswith_no_case(t, "Provides:")) {
632 FOREACH_WORD_QUOTED(w, z, t+9, i) {
635 if (!(n = strndup(w, z))) {
640 r = sysv_translate_facility(n, file_name_from_path(path), &m);
649 if (unit_name_to_type(m) == UNIT_SERVICE)
650 r = unit_add_name(u, m);
652 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
654 if (s->sysv_enabled) {
657 if ((k = unit_add_dependency_by_name_inverse(u, UNIT_WANTS, m, NULL, true)) < 0)
663 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
668 } else if (startswith_no_case(t, "Required-Start:") ||
669 startswith_no_case(t, "Should-Start:") ||
670 startswith_no_case(t, "X-Start-Before:") ||
671 startswith_no_case(t, "X-Start-After:")) {
677 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
680 if (!(n = strndup(w, z))) {
685 r = sysv_translate_facility(n, file_name_from_path(path), &m);
688 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
698 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
701 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
705 } else if (startswith_no_case(t, "Default-Start:")) {
710 k = delete_chars(t+14, WHITESPACE "-");
713 if (!(d = strdup(k))) {
718 free(s->sysv_runlevels);
719 s->sysv_runlevels = d;
722 } else if (startswith_no_case(t, "Description:")) {
725 state = LSB_DESCRIPTION;
727 if ((j = strstrip(t+12)) && *j) {
728 if (!(d = strdup(j))) {
735 free(long_description);
736 long_description = d;
738 } else if (startswith_no_case(t, "Short-Description:")) {
743 if ((j = strstrip(t+18)) && *j) {
744 if (!(d = strdup(j))) {
751 free(short_description);
752 short_description = d;
754 } else if (startswith_no_case(t, "X-Interactive:")) {
757 if ((b = parse_boolean(strstrip(t+14))) < 0) {
758 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
763 s->exec_context.std_input = EXEC_INPUT_TTY;
765 s->exec_context.std_input = EXEC_INPUT_NULL;
767 } else if (state == LSB_DESCRIPTION) {
769 if (startswith(l, "#\t") || startswith(l, "# ")) {
772 if ((j = strstrip(t)) && *j) {
775 if (long_description)
776 asprintf(&d, "%s %s", long_description, t);
785 free(long_description);
786 long_description = d;
795 if ((r = sysv_exec_commands(s)) < 0)
798 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
799 /* If there a runlevels configured for this service
800 * but none of the standard ones, then we assume this
801 * is some special kind of service (which might be
802 * needed for early boot) and don't create any links
805 s->meta.default_dependencies = false;
807 /* Don't timeout special services during boot (like fsck) */
810 s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC;
812 /* Special setting for all SysV services */
813 s->type = SERVICE_FORKING;
814 s->remain_after_exit = true;
815 s->restart = SERVICE_RESTART_NO;
816 s->exec_context.std_output =
817 (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY)
818 ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
819 s->exec_context.kill_mode = KILL_PROCESS_GROUP;
821 /* We use the long description only if
822 * no short description is set. */
824 if (short_description)
825 description = short_description;
826 else if (chkconfig_description)
827 description = chkconfig_description;
828 else if (long_description)
829 description = long_description;
836 if (!(d = strappend("LSB: ", description))) {
841 u->meta.description = d;
844 u->meta.load_state = UNIT_LOADED;
852 free(short_description);
853 free(long_description);
854 free(chkconfig_description);
859 static int service_load_sysv_name(Service *s, const char *name) {
865 /* For SysV services we strip the boot.*, rc.* and *.sh
866 * prefixes/suffixes. */
867 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
868 if (endswith(name, ".sh.service"))
873 if (startswith(name, "boot."))
877 #ifdef TARGET_FRUGALWARE
878 if (startswith(name, "rc."))
882 STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
886 if (asprintf(&path, "%s/%s", *p, name) < 0)
889 assert(endswith(path, ".service"));
890 path[strlen(path)-8] = 0;
892 r = service_load_sysv_path(s, path);
894 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
895 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
896 /* Try Debian style *.sh source'able init scripts */
898 r = service_load_sysv_path(s, path);
904 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
905 /* Try SUSE style boot.* init scripts */
907 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
910 /* Drop .service suffix */
911 path[strlen(path)-8] = 0;
912 r = service_load_sysv_path(s, path);
917 #ifdef TARGET_FRUGALWARE
918 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
919 /* Try Frugalware style rc.* init scripts */
921 if (asprintf(&path, "%s/rc.%s", *p, name) < 0)
924 /* Drop .service suffix */
925 path[strlen(path)-8] = 0;
926 r = service_load_sysv_path(s, path);
934 if ((s->meta.load_state != UNIT_STUB))
941 static int service_load_sysv(Service *s) {
948 /* Load service data from SysV init scripts, preferably with
951 if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
954 if ((t = s->meta.id))
955 if ((r = service_load_sysv_name(s, t)) < 0)
958 if (s->meta.load_state == UNIT_STUB)
959 SET_FOREACH(t, s->meta.names, i) {
963 if ((r = service_load_sysv_name(s, t)) < 0)
966 if (s->meta.load_state != UNIT_STUB)
974 static int fsck_fix_order(Service *s) {
980 if (s->fsck_passno <= 0)
983 /* For each pair of services where both have an fsck priority
984 * we order things based on it. */
986 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
990 t = (Service*) other;
995 if (t->meta.load_state != UNIT_LOADED)
998 if (t->fsck_passno <= 0)
1001 if (t->fsck_passno < s->fsck_passno)
1003 else if (t->fsck_passno > s->fsck_passno)
1008 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1015 static int service_verify(Service *s) {
1018 if (s->meta.load_state != UNIT_LOADED)
1021 if (!s->exec_command[SERVICE_EXEC_START]) {
1022 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
1026 if (s->type != SERVICE_ONESHOT &&
1027 s->exec_command[SERVICE_EXEC_START]->command_next) {
1028 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
1032 if (s->type == SERVICE_DBUS && !s->bus_name) {
1033 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
1037 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
1038 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
1045 static int service_add_default_dependencies(Service *s) {
1050 /* Add a number of automatic dependencies useful for the
1051 * majority of services. */
1053 /* First, pull in base system */
1054 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
1056 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1059 } else if (s->meta.manager->running_as == MANAGER_USER) {
1061 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1065 /* Second, activate normal shutdown */
1066 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1069 static int service_load(Unit *u) {
1071 Service *s = SERVICE(u);
1075 /* Load a .service file */
1076 if ((r = unit_load_fragment(u)) < 0)
1079 #ifdef HAVE_SYSV_COMPAT
1080 /* Load a classic init script as a fallback, if we couldn't find anything */
1081 if (u->meta.load_state == UNIT_STUB)
1082 if ((r = service_load_sysv(s)) < 0)
1086 /* Still nothing found? Then let's give up */
1087 if (u->meta.load_state == UNIT_STUB)
1090 /* We were able to load something, then let's add in the
1091 * dropin directories. */
1092 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1095 /* This is a new unit? Then let's add in some extras */
1096 if (u->meta.load_state == UNIT_LOADED) {
1097 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1100 if ((r = unit_add_default_cgroups(u)) < 0)
1103 #ifdef HAVE_SYSV_COMPAT
1104 if ((r = sysv_fix_order(s)) < 0)
1108 if ((r = fsck_fix_order(s)) < 0)
1112 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1115 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1116 s->notify_access = NOTIFY_MAIN;
1118 if (s->type == SERVICE_DBUS || s->bus_name)
1119 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
1122 if (s->meta.default_dependencies)
1123 if ((r = service_add_default_dependencies(s)) < 0)
1127 return service_verify(s);
1130 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1132 ServiceExecCommand c;
1133 Service *s = SERVICE(u);
1134 const char *prefix2;
1139 p2 = strappend(prefix, "\t");
1140 prefix2 = p2 ? p2 : prefix;
1143 "%sService State: %s\n"
1144 "%sPermissionsStartOnly: %s\n"
1145 "%sRootDirectoryStartOnly: %s\n"
1146 "%sRemainAfterExit: %s\n"
1149 "%sNotifyAccess: %s\n",
1150 prefix, service_state_to_string(s->state),
1151 prefix, yes_no(s->permissions_start_only),
1152 prefix, yes_no(s->root_directory_start_only),
1153 prefix, yes_no(s->remain_after_exit),
1154 prefix, service_type_to_string(s->type),
1155 prefix, service_restart_to_string(s->restart),
1156 prefix, notify_access_to_string(s->notify_access));
1158 if (s->control_pid > 0)
1160 "%sControl PID: %lu\n",
1161 prefix, (unsigned long) s->control_pid);
1163 if (s->main_pid > 0)
1165 "%sMain PID: %lu\n",
1166 prefix, (unsigned long) s->main_pid);
1171 prefix, s->pid_file);
1176 "%sBus Name Good: %s\n",
1177 prefix, s->bus_name,
1178 prefix, yes_no(s->bus_name_good));
1180 exec_context_dump(&s->exec_context, f, prefix);
1182 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1184 if (!s->exec_command[c])
1187 fprintf(f, "%s-> %s:\n",
1188 prefix, service_exec_command_to_string(c));
1190 exec_command_dump_list(s->exec_command[c], f, prefix2);
1193 #ifdef HAVE_SYSV_COMPAT
1196 "%sSysV Init Script Path: %s\n"
1197 "%sSysV Init Script has LSB Header: %s\n"
1198 "%sSysVEnabled: %s\n",
1199 prefix, s->sysv_path,
1200 prefix, yes_no(s->sysv_has_lsb),
1201 prefix, yes_no(s->sysv_enabled));
1203 if (s->sysv_start_priority >= 0)
1205 "%sSysVStartPriority: %i\n",
1206 prefix, s->sysv_start_priority);
1208 if (s->sysv_runlevels)
1209 fprintf(f, "%sSysVRunLevels: %s\n",
1210 prefix, s->sysv_runlevels);
1213 if (s->fsck_passno > 0)
1215 "%sFsckPassNo: %i\n",
1216 prefix, s->fsck_passno);
1219 fprintf(f, "%sStatus Text: %s\n",
1220 prefix, s->status_text);
1225 static int service_load_pid_file(Service *s) {
1232 if (s->main_pid_known)
1235 assert(s->main_pid <= 0);
1240 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1243 r = parse_pid(k, &pid);
1249 if (kill(pid, 0) < 0 && errno != EPERM) {
1250 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1251 (unsigned long) pid, s->pid_file);
1255 if ((r = service_set_main_pid(s, pid)) < 0)
1258 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1259 /* FIXME: we need to do something here */
1265 static int service_search_main_pid(Service *s) {
1271 if (s->main_pid_known)
1274 assert(s->main_pid <= 0);
1276 if ((pid = cgroup_bonding_search_main_pid_list(s->meta.cgroup_bondings)) <= 0)
1279 if ((r = service_set_main_pid(s, pid)) < 0)
1282 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1283 /* FIXME: we need to do something here */
1289 static int service_get_sockets(Service *s, Set **_set) {
1298 if (s->socket_fd >= 0)
1301 if (!set_isempty(s->configured_sockets))
1304 /* Collects all Socket objects that belong to this
1305 * service. Note that a service might have multiple sockets
1306 * via multiple names. */
1308 if (!(set = set_new(NULL, NULL)))
1311 SET_FOREACH(t, s->meta.names, i) {
1315 /* Look for all socket objects that go by any of our
1316 * units and collect their fds */
1318 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1323 p = manager_get_unit(s->meta.manager, k);
1329 if ((r = set_put(set, p)) < 0)
1341 static int service_notify_sockets_dead(Service *s) {
1343 Set *set, *free_set = NULL;
1349 /* Notifies all our sockets when we die */
1351 if (s->socket_fd >= 0)
1354 if (!set_isempty(s->configured_sockets))
1355 set = s->configured_sockets;
1357 if ((r = service_get_sockets(s, &free_set)) < 0)
1363 SET_FOREACH(sock, set, i)
1364 socket_notify_service_dead(sock);
1371 static void service_set_state(Service *s, ServiceState state) {
1372 ServiceState old_state;
1375 old_state = s->state;
1378 if (state != SERVICE_START_PRE &&
1379 state != SERVICE_START &&
1380 state != SERVICE_START_POST &&
1381 state != SERVICE_RELOAD &&
1382 state != SERVICE_STOP &&
1383 state != SERVICE_STOP_SIGTERM &&
1384 state != SERVICE_STOP_SIGKILL &&
1385 state != SERVICE_STOP_POST &&
1386 state != SERVICE_FINAL_SIGTERM &&
1387 state != SERVICE_FINAL_SIGKILL &&
1388 state != SERVICE_AUTO_RESTART)
1389 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1391 if (state != SERVICE_START &&
1392 state != SERVICE_START_POST &&
1393 state != SERVICE_RUNNING &&
1394 state != SERVICE_RELOAD &&
1395 state != SERVICE_STOP &&
1396 state != SERVICE_STOP_SIGTERM &&
1397 state != SERVICE_STOP_SIGKILL)
1398 service_unwatch_main_pid(s);
1400 if (state != SERVICE_START_PRE &&
1401 state != SERVICE_START &&
1402 state != SERVICE_START_POST &&
1403 state != SERVICE_RELOAD &&
1404 state != SERVICE_STOP &&
1405 state != SERVICE_STOP_SIGTERM &&
1406 state != SERVICE_STOP_SIGKILL &&
1407 state != SERVICE_STOP_POST &&
1408 state != SERVICE_FINAL_SIGTERM &&
1409 state != SERVICE_FINAL_SIGKILL) {
1410 service_unwatch_control_pid(s);
1411 s->control_command = NULL;
1412 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1415 if (state == SERVICE_DEAD ||
1416 state == SERVICE_STOP ||
1417 state == SERVICE_STOP_SIGTERM ||
1418 state == SERVICE_STOP_SIGKILL ||
1419 state == SERVICE_STOP_POST ||
1420 state == SERVICE_FINAL_SIGTERM ||
1421 state == SERVICE_FINAL_SIGKILL ||
1422 state == SERVICE_FAILED ||
1423 state == SERVICE_AUTO_RESTART)
1424 service_notify_sockets_dead(s);
1426 if (state != SERVICE_START_PRE &&
1427 state != SERVICE_START &&
1428 state != SERVICE_START_POST &&
1429 state != SERVICE_RUNNING &&
1430 state != SERVICE_RELOAD &&
1431 state != SERVICE_STOP &&
1432 state != SERVICE_STOP_SIGTERM &&
1433 state != SERVICE_STOP_SIGKILL &&
1434 state != SERVICE_STOP_POST &&
1435 state != SERVICE_FINAL_SIGTERM &&
1436 state != SERVICE_FINAL_SIGKILL &&
1437 !(state == SERVICE_DEAD && s->meta.job)) {
1438 service_close_socket_fd(s);
1439 service_connection_unref(s);
1442 /* For the inactive states unit_notify() will trim the cgroup,
1443 * but for exit we have to do that ourselves... */
1444 if (state == SERVICE_EXITED)
1445 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1447 if (old_state != state)
1448 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1450 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], !s->reload_failure);
1451 s->reload_failure = false;
1454 static int service_coldplug(Unit *u) {
1455 Service *s = SERVICE(u);
1459 assert(s->state == SERVICE_DEAD);
1461 if (s->deserialized_state != s->state) {
1463 if (s->deserialized_state == SERVICE_START_PRE ||
1464 s->deserialized_state == SERVICE_START ||
1465 s->deserialized_state == SERVICE_START_POST ||
1466 s->deserialized_state == SERVICE_RELOAD ||
1467 s->deserialized_state == SERVICE_STOP ||
1468 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1469 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1470 s->deserialized_state == SERVICE_STOP_POST ||
1471 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1472 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1473 s->deserialized_state == SERVICE_AUTO_RESTART) {
1475 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1478 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1480 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1485 if ((s->deserialized_state == SERVICE_START &&
1486 (s->type == SERVICE_FORKING ||
1487 s->type == SERVICE_DBUS ||
1488 s->type == SERVICE_ONESHOT ||
1489 s->type == SERVICE_NOTIFY)) ||
1490 s->deserialized_state == SERVICE_START_POST ||
1491 s->deserialized_state == SERVICE_RUNNING ||
1492 s->deserialized_state == SERVICE_RELOAD ||
1493 s->deserialized_state == SERVICE_STOP ||
1494 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1495 s->deserialized_state == SERVICE_STOP_SIGKILL)
1496 if (s->main_pid > 0)
1497 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1500 if (s->deserialized_state == SERVICE_START_PRE ||
1501 s->deserialized_state == SERVICE_START ||
1502 s->deserialized_state == SERVICE_START_POST ||
1503 s->deserialized_state == SERVICE_RELOAD ||
1504 s->deserialized_state == SERVICE_STOP ||
1505 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1506 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1507 s->deserialized_state == SERVICE_STOP_POST ||
1508 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1509 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1510 if (s->control_pid > 0)
1511 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1514 service_set_state(s, s->deserialized_state);
1520 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1524 unsigned rn_fds = 0;
1525 Set *set, *free_set = NULL;
1532 if (s->socket_fd >= 0)
1535 if (!set_isempty(s->configured_sockets))
1536 set = s->configured_sockets;
1538 if ((r = service_get_sockets(s, &free_set)) < 0)
1544 SET_FOREACH(sock, set, i) {
1548 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1560 if (!(t = new(int, rn_fds+cn_fds))) {
1566 memcpy(t, rfds, rn_fds);
1567 memcpy(t+rn_fds, cfds, cn_fds);
1572 rn_fds = rn_fds+cn_fds;
1590 static int service_spawn(
1595 bool apply_permissions,
1597 bool apply_tty_stdin,
1598 bool set_notify_socket,
1603 int *fds = NULL, *fdsbuf = NULL;
1604 unsigned n_fds = 0, n_env = 0;
1605 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1612 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1613 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1614 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1616 if (s->socket_fd >= 0) {
1617 fds = &s->socket_fd;
1620 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1627 if (timeout && s->timeout_usec) {
1628 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1631 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1633 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1638 if (!(our_env = new0(char*, 4))) {
1643 if (set_notify_socket)
1644 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1649 if (s->main_pid > 0)
1650 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1655 if (!(final_env = strv_env_merge(2,
1656 s->meta.manager->environment,
1671 s->meta.manager->confirm_spawn,
1672 s->meta.cgroup_bondings,
1679 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1680 /* FIXME: we need to do something here */
1686 strv_free(final_env);
1696 strv_free(final_env);
1699 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1704 static int main_pid_good(Service *s) {
1707 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1710 /* If we know the pid file, then lets just check if it is
1712 if (s->main_pid_known)
1713 return s->main_pid > 0;
1715 /* We don't know the pid */
1719 static int control_pid_good(Service *s) {
1722 return s->control_pid > 0;
1725 static int cgroup_good(Service *s) {
1730 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1736 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1743 if (allow_restart &&
1744 !s->forbid_restart &&
1745 (s->restart == SERVICE_RESTART_ALWAYS ||
1746 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure) ||
1747 (s->restart == SERVICE_RESTART_ON_FAILURE && s->failure) ||
1748 (s->restart == SERVICE_RESTART_ON_ABORT && s->failure &&
1749 (s->main_exec_status.code == CLD_KILLED ||
1750 s->main_exec_status.code == CLD_DUMPED)))) {
1752 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1755 service_set_state(s, SERVICE_AUTO_RESTART);
1757 service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD);
1759 s->forbid_restart = false;
1764 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1765 service_enter_dead(s, false, false);
1768 static void service_enter_signal(Service *s, ServiceState state, bool success);
1770 static void service_enter_stop_post(Service *s, bool success) {
1777 service_unwatch_control_pid(s);
1779 s->control_command_id = SERVICE_EXEC_STOP_POST;
1780 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1781 if ((r = service_spawn(s,
1785 !s->permissions_start_only,
1786 !s->root_directory_start_only,
1789 &s->control_pid)) < 0)
1793 service_set_state(s, SERVICE_STOP_POST);
1795 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1800 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1801 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1804 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1806 Set *pid_set = NULL;
1807 bool wait_for_exit = false;
1814 if (s->exec_context.kill_mode != KILL_NONE) {
1815 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1817 if (s->main_pid > 0) {
1818 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1820 s->main_pid, sig) < 0 && errno != ESRCH)
1822 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1824 wait_for_exit = true;
1827 if (s->control_pid > 0) {
1828 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1830 s->control_pid, sig) < 0 && errno != ESRCH)
1832 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1834 wait_for_exit = true;
1837 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1839 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1844 /* Exclude the main/control pids from being killed via the cgroup */
1845 if (s->main_pid > 0)
1846 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1849 if (s->control_pid > 0)
1850 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1853 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
1854 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1855 log_warning("Failed to kill control group: %s", strerror(-r));
1857 wait_for_exit = true;
1863 if (wait_for_exit) {
1864 if (s->timeout_usec > 0)
1865 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1868 service_set_state(s, state);
1869 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1870 service_enter_stop_post(s, true);
1872 service_enter_dead(s, true, true);
1877 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1879 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1880 service_enter_stop_post(s, false);
1882 service_enter_dead(s, false, true);
1888 static void service_enter_stop(Service *s, bool success) {
1896 service_unwatch_control_pid(s);
1898 s->control_command_id = SERVICE_EXEC_STOP;
1899 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1900 if ((r = service_spawn(s,
1904 !s->permissions_start_only,
1905 !s->root_directory_start_only,
1908 &s->control_pid)) < 0)
1911 service_set_state(s, SERVICE_STOP);
1913 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1918 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1919 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1922 static void service_enter_running(Service *s, bool success) {
1923 int main_pid_ok, cgroup_ok;
1929 main_pid_ok = main_pid_good(s);
1930 cgroup_ok = cgroup_good(s);
1932 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1933 (s->bus_name_good || s->type != SERVICE_DBUS))
1934 service_set_state(s, SERVICE_RUNNING);
1935 else if (s->remain_after_exit)
1936 service_set_state(s, SERVICE_EXITED);
1938 service_enter_stop(s, true);
1941 static void service_enter_start_post(Service *s) {
1945 service_unwatch_control_pid(s);
1947 s->control_command_id = SERVICE_EXEC_START_POST;
1948 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1949 if ((r = service_spawn(s,
1953 !s->permissions_start_only,
1954 !s->root_directory_start_only,
1957 &s->control_pid)) < 0)
1960 service_set_state(s, SERVICE_START_POST);
1962 service_enter_running(s, true);
1967 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1968 service_enter_stop(s, false);
1971 static void service_enter_start(Service *s) {
1977 assert(s->exec_command[SERVICE_EXEC_START]);
1978 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1980 if (s->type == SERVICE_FORKING)
1981 service_unwatch_control_pid(s);
1983 service_unwatch_main_pid(s);
1985 s->control_command_id = SERVICE_EXEC_START;
1986 s->control_command = s->exec_command[SERVICE_EXEC_START];
1988 if ((r = service_spawn(s,
1990 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1995 s->notify_access != NOTIFY_NONE,
1999 if (s->type == SERVICE_SIMPLE) {
2000 /* For simple services we immediately start
2001 * the START_POST binaries. */
2003 service_set_main_pid(s, pid);
2004 service_enter_start_post(s);
2006 } else if (s->type == SERVICE_FORKING) {
2008 /* For forking services we wait until the start
2009 * process exited. */
2011 s->control_pid = pid;
2012 service_set_state(s, SERVICE_START);
2014 } else if (s->type == SERVICE_ONESHOT ||
2015 s->type == SERVICE_DBUS ||
2016 s->type == SERVICE_NOTIFY) {
2018 /* For oneshot services we wait until the start
2019 * process exited, too, but it is our main process. */
2021 /* For D-Bus services we know the main pid right away,
2022 * but wait for the bus name to appear on the
2023 * bus. Notify services are similar. */
2025 service_set_main_pid(s, pid);
2026 service_set_state(s, SERVICE_START);
2028 assert_not_reached("Unknown service type");
2033 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
2034 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2037 static void service_enter_start_pre(Service *s) {
2042 service_unwatch_control_pid(s);
2044 s->control_command_id = SERVICE_EXEC_START_PRE;
2045 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2046 if ((r = service_spawn(s,
2050 !s->permissions_start_only,
2051 !s->root_directory_start_only,
2054 &s->control_pid)) < 0)
2057 service_set_state(s, SERVICE_START_PRE);
2059 service_enter_start(s);
2064 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
2065 service_enter_dead(s, false, true);
2068 static void service_enter_restart(Service *s) {
2073 dbus_error_init(&error);
2076 log_info("Job pending for unit, delaying automatic restart.");
2078 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
2082 service_enter_dead(s, true, false);
2084 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
2087 log_debug("%s scheduled restart job.", s->meta.id);
2091 log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
2092 service_enter_dead(s, false, false);
2094 dbus_error_free(&error);
2097 static void service_enter_reload(Service *s) {
2102 service_unwatch_control_pid(s);
2104 s->control_command_id = SERVICE_EXEC_RELOAD;
2105 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2106 if ((r = service_spawn(s,
2110 !s->permissions_start_only,
2111 !s->root_directory_start_only,
2114 &s->control_pid)) < 0)
2117 service_set_state(s, SERVICE_RELOAD);
2119 service_enter_running(s, true);
2124 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
2125 s->reload_failure = true;
2126 service_enter_running(s, true);
2129 static void service_run_next_control(Service *s, bool success) {
2133 assert(s->control_command);
2134 assert(s->control_command->command_next);
2139 assert(s->control_command_id != SERVICE_EXEC_START);
2141 s->control_command = s->control_command->command_next;
2142 service_unwatch_control_pid(s);
2144 if ((r = service_spawn(s,
2148 !s->permissions_start_only,
2149 !s->root_directory_start_only,
2150 s->control_command_id == SERVICE_EXEC_START_PRE ||
2151 s->control_command_id == SERVICE_EXEC_STOP_POST,
2153 &s->control_pid)) < 0)
2159 log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
2161 if (s->state == SERVICE_START_PRE)
2162 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2163 else if (s->state == SERVICE_STOP)
2164 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2165 else if (s->state == SERVICE_STOP_POST)
2166 service_enter_dead(s, false, true);
2167 else if (s->state == SERVICE_RELOAD) {
2168 s->reload_failure = true;
2169 service_enter_running(s, true);
2171 service_enter_stop(s, false);
2174 static void service_run_next_main(Service *s, bool success) {
2179 assert(s->control_command);
2180 assert(s->control_command->command_next);
2185 assert(s->control_command_id == SERVICE_EXEC_START);
2186 assert(s->type == SERVICE_ONESHOT);
2188 s->control_command = s->control_command->command_next;
2189 service_unwatch_main_pid(s);
2191 if ((r = service_spawn(s,
2198 s->notify_access != NOTIFY_NONE,
2202 service_set_main_pid(s, pid);
2207 log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
2208 service_enter_stop(s, false);
2211 static int service_start(Unit *u) {
2212 Service *s = SERVICE(u);
2216 /* We cannot fulfill this request right now, try again later
2218 if (s->state == SERVICE_STOP ||
2219 s->state == SERVICE_STOP_SIGTERM ||
2220 s->state == SERVICE_STOP_SIGKILL ||
2221 s->state == SERVICE_STOP_POST ||
2222 s->state == SERVICE_FINAL_SIGTERM ||
2223 s->state == SERVICE_FINAL_SIGKILL)
2226 /* Already on it! */
2227 if (s->state == SERVICE_START_PRE ||
2228 s->state == SERVICE_START ||
2229 s->state == SERVICE_START_POST)
2232 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
2234 /* Make sure we don't enter a busy loop of some kind. */
2235 if (!ratelimit_test(&s->ratelimit)) {
2236 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
2241 s->main_pid_known = false;
2242 s->forbid_restart = false;
2244 service_enter_start_pre(s);
2248 static int service_stop(Unit *u) {
2249 Service *s = SERVICE(u);
2253 /* This is a user request, so don't do restarts on this
2255 s->forbid_restart = true;
2258 if (s->state == SERVICE_STOP ||
2259 s->state == SERVICE_STOP_SIGTERM ||
2260 s->state == SERVICE_STOP_SIGKILL ||
2261 s->state == SERVICE_STOP_POST ||
2262 s->state == SERVICE_FINAL_SIGTERM ||
2263 s->state == SERVICE_FINAL_SIGKILL)
2266 /* Don't allow a restart */
2267 if (s->state == SERVICE_AUTO_RESTART) {
2268 service_set_state(s, SERVICE_DEAD);
2272 /* If there's already something running we go directly into
2274 if (s->state == SERVICE_START_PRE ||
2275 s->state == SERVICE_START ||
2276 s->state == SERVICE_START_POST ||
2277 s->state == SERVICE_RELOAD) {
2278 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2282 assert(s->state == SERVICE_RUNNING ||
2283 s->state == SERVICE_EXITED);
2285 service_enter_stop(s, true);
2289 static int service_reload(Unit *u) {
2290 Service *s = SERVICE(u);
2294 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2296 service_enter_reload(s);
2300 static bool service_can_reload(Unit *u) {
2301 Service *s = SERVICE(u);
2305 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2308 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2309 Service *s = SERVICE(u);
2315 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2316 unit_serialize_item(u, f, "failure", yes_no(s->failure));
2318 if (s->control_pid > 0)
2319 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2321 if (s->main_pid_known && s->main_pid > 0)
2322 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2324 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2327 unit_serialize_item(u, f, "status-text", s->status_text);
2329 /* There's a minor uncleanliness here: if there are multiple
2330 * commands attached here, we will start from the first one
2332 if (s->control_command_id >= 0)
2333 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2335 if (s->socket_fd >= 0) {
2338 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2341 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2344 if (s->main_exec_status.pid > 0) {
2345 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2346 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2347 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2349 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2350 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2351 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2358 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2359 Service *s = SERVICE(u);
2366 if (streq(key, "state")) {
2369 if ((state = service_state_from_string(value)) < 0)
2370 log_debug("Failed to parse state value %s", value);
2372 s->deserialized_state = state;
2373 } else if (streq(key, "failure")) {
2376 if ((b = parse_boolean(value)) < 0)
2377 log_debug("Failed to parse failure value %s", value);
2379 s->failure = b || s->failure;
2380 } else if (streq(key, "control-pid")) {
2383 if (parse_pid(value, &pid) < 0)
2384 log_debug("Failed to parse control-pid value %s", value);
2386 s->control_pid = pid;
2387 } else if (streq(key, "main-pid")) {
2390 if (parse_pid(value, &pid) < 0)
2391 log_debug("Failed to parse main-pid value %s", value);
2393 service_set_main_pid(s, (pid_t) pid);
2394 } else if (streq(key, "main-pid-known")) {
2397 if ((b = parse_boolean(value)) < 0)
2398 log_debug("Failed to parse main-pid-known value %s", value);
2400 s->main_pid_known = b;
2401 } else if (streq(key, "status-text")) {
2404 if ((t = strdup(value))) {
2405 free(s->status_text);
2409 } else if (streq(key, "control-command")) {
2410 ServiceExecCommand id;
2412 if ((id = service_exec_command_from_string(value)) < 0)
2413 log_debug("Failed to parse exec-command value %s", value);
2415 s->control_command_id = id;
2416 s->control_command = s->exec_command[id];
2418 } else if (streq(key, "socket-fd")) {
2421 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2422 log_debug("Failed to parse socket-fd value %s", value);
2425 if (s->socket_fd >= 0)
2426 close_nointr_nofail(s->socket_fd);
2427 s->socket_fd = fdset_remove(fds, fd);
2429 } else if (streq(key, "main-exec-status-pid")) {
2432 if (parse_pid(value, &pid) < 0)
2433 log_debug("Failed to parse main-exec-status-pid value %s", value);
2435 s->main_exec_status.pid = pid;
2436 } else if (streq(key, "main-exec-status-code")) {
2439 if (safe_atoi(value, &i) < 0)
2440 log_debug("Failed to parse main-exec-status-code value %s", value);
2442 s->main_exec_status.code = i;
2443 } else if (streq(key, "main-exec-status-status")) {
2446 if (safe_atoi(value, &i) < 0)
2447 log_debug("Failed to parse main-exec-status-status value %s", value);
2449 s->main_exec_status.status = i;
2450 } else if (streq(key, "main-exec-status-start"))
2451 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2452 else if (streq(key, "main-exec-status-exit"))
2453 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2455 log_debug("Unknown serialization key '%s'", key);
2460 static UnitActiveState service_active_state(Unit *u) {
2463 return state_translation_table[SERVICE(u)->state];
2466 static const char *service_sub_state_to_string(Unit *u) {
2469 return service_state_to_string(SERVICE(u)->state);
2472 #ifdef HAVE_SYSV_COMPAT
2473 static bool service_check_gc(Unit *u) {
2474 Service *s = SERVICE(u);
2478 return !!s->sysv_path;
2482 static bool service_check_snapshot(Unit *u) {
2483 Service *s = SERVICE(u);
2487 return !s->got_socket_fd;
2490 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2491 Service *s = SERVICE(u);
2497 if (!s->meta.fragment_path)
2498 success = is_clean_exit_lsb(code, status);
2500 success = is_clean_exit(code, status);
2502 if (s->main_pid == pid) {
2505 exec_status_exit(&s->main_exec_status, pid, code, status, s->exec_context.utmp_id);
2507 if (s->type != SERVICE_FORKING && s->control_command) {
2508 s->control_command->exec_status = s->main_exec_status;
2510 if (s->control_command->ignore)
2514 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2515 "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2516 s->failure = s->failure || !success;
2518 if (s->control_command &&
2519 s->control_command->command_next &&
2522 /* There is another command to *
2523 * execute, so let's do that. */
2525 log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2526 service_run_next_main(s, success);
2530 /* The service exited, so the service is officially
2533 s->control_command = NULL;
2534 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2538 case SERVICE_START_POST:
2539 case SERVICE_RELOAD:
2541 /* Need to wait until the operation is
2546 if (s->type == SERVICE_ONESHOT) {
2547 /* This was our main goal, so let's go on */
2549 service_enter_start_post(s);
2551 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2554 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2559 case SERVICE_RUNNING:
2560 service_enter_running(s, success);
2563 case SERVICE_STOP_SIGTERM:
2564 case SERVICE_STOP_SIGKILL:
2566 if (!control_pid_good(s))
2567 service_enter_stop_post(s, success);
2569 /* If there is still a control process, wait for that first */
2573 assert_not_reached("Uh, main process died at wrong time.");
2577 } else if (s->control_pid == pid) {
2581 if (s->control_command) {
2582 exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id);
2584 if (s->control_command->ignore)
2588 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2589 "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2590 s->failure = s->failure || !success;
2592 if (s->control_command &&
2593 s->control_command->command_next &&
2596 /* There is another command to *
2597 * execute, so let's do that. */
2599 log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2600 service_run_next_control(s, success);
2603 /* No further commands for this step, so let's
2604 * figure out what to do next */
2606 s->control_command = NULL;
2607 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2609 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2613 case SERVICE_START_PRE:
2615 service_enter_start(s);
2617 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2621 assert(s->type == SERVICE_FORKING);
2623 /* Let's try to load the pid
2624 * file here if we can. We
2625 * ignore the return value,
2626 * since the PID file might
2627 * actually be created by a
2628 * START_POST script */
2631 service_load_pid_file(s);
2632 service_search_main_pid(s);
2634 service_enter_start_post(s);
2636 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2640 case SERVICE_START_POST:
2641 if (success && s->pid_file && !s->main_pid_known) {
2644 /* Hmm, let's see if we can
2645 * load the pid now after the
2646 * start-post scripts got
2649 if ((r = service_load_pid_file(s)) < 0)
2650 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2655 case SERVICE_RELOAD:
2656 s->reload_failure = !success;
2657 service_enter_running(s, true);
2661 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2664 case SERVICE_STOP_SIGTERM:
2665 case SERVICE_STOP_SIGKILL:
2666 if (main_pid_good(s) <= 0)
2667 service_enter_stop_post(s, success);
2669 /* If there is still a service
2670 * process around, wait until
2671 * that one quit, too */
2674 case SERVICE_STOP_POST:
2675 case SERVICE_FINAL_SIGTERM:
2676 case SERVICE_FINAL_SIGKILL:
2677 service_enter_dead(s, success, true);
2681 assert_not_reached("Uh, control process died at wrong time.");
2686 /* Notify clients about changed exit status */
2687 unit_add_to_dbus_queue(u);
2690 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2691 Service *s = SERVICE(u);
2694 assert(elapsed == 1);
2696 assert(w == &s->timer_watch);
2700 case SERVICE_START_PRE:
2702 log_warning("%s operation timed out. Terminating.", u->meta.id);
2703 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2706 case SERVICE_START_POST:
2707 log_warning("%s operation timed out. Stopping.", u->meta.id);
2708 service_enter_stop(s, false);
2711 case SERVICE_RELOAD:
2712 log_warning("%s operation timed out. Stopping.", u->meta.id);
2713 s->reload_failure = true;
2714 service_enter_running(s, true);
2718 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2719 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2722 case SERVICE_STOP_SIGTERM:
2723 if (s->exec_context.send_sigkill) {
2724 log_warning("%s stopping timed out. Killing.", u->meta.id);
2725 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2727 log_warning("%s stopping timed out. Skipping SIGKILL.", u->meta.id);
2728 service_enter_stop_post(s, false);
2733 case SERVICE_STOP_SIGKILL:
2734 /* Uh, wie sent a SIGKILL and it is still not gone?
2735 * Must be something we cannot kill, so let's just be
2736 * weirded out and continue */
2738 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2739 service_enter_stop_post(s, false);
2742 case SERVICE_STOP_POST:
2743 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2744 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2747 case SERVICE_FINAL_SIGTERM:
2748 if (s->exec_context.send_sigkill) {
2749 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2750 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2752 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->meta.id);
2753 service_enter_dead(s, false, true);
2758 case SERVICE_FINAL_SIGKILL:
2759 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
2760 service_enter_dead(s, false, true);
2763 case SERVICE_AUTO_RESTART:
2764 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2765 service_enter_restart(s);
2769 assert_not_reached("Timeout at wrong time.");
2773 static void service_cgroup_notify_event(Unit *u) {
2774 Service *s = SERVICE(u);
2778 log_debug("%s: cgroup is empty", u->meta.id);
2782 /* Waiting for SIGCHLD is usually more interesting,
2783 * because it includes return codes/signals. Which is
2784 * why we ignore the cgroup events for most cases,
2785 * except when we don't know pid which to expect the
2788 case SERVICE_RUNNING:
2789 service_enter_running(s, true);
2792 case SERVICE_STOP_SIGTERM:
2793 case SERVICE_STOP_SIGKILL:
2794 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2795 service_enter_stop_post(s, true);
2799 case SERVICE_FINAL_SIGTERM:
2800 case SERVICE_FINAL_SIGKILL:
2801 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2802 service_enter_dead(s, true, true);
2811 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2812 Service *s = SERVICE(u);
2817 if (s->notify_access == NOTIFY_NONE) {
2818 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2819 u->meta.id, (unsigned long) pid);
2823 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2824 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2825 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2829 log_debug("%s: Got message", u->meta.id);
2831 /* Interpret MAINPID= */
2832 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2833 (s->state == SERVICE_START ||
2834 s->state == SERVICE_START_POST ||
2835 s->state == SERVICE_RUNNING ||
2836 s->state == SERVICE_RELOAD)) {
2838 if (parse_pid(e + 8, &pid) < 0)
2839 log_warning("Failed to parse notification message %s", e);
2841 log_debug("%s: got %s", u->meta.id, e);
2842 service_set_main_pid(s, pid);
2846 /* Interpret READY= */
2847 if (s->type == SERVICE_NOTIFY &&
2848 s->state == SERVICE_START &&
2849 strv_find(tags, "READY=1")) {
2850 log_debug("%s: got READY=1", u->meta.id);
2852 service_enter_start_post(s);
2855 /* Interpret STATUS= */
2856 if ((e = strv_find_prefix(tags, "STATUS="))) {
2860 if (!(t = strdup(e+7))) {
2861 log_error("Failed to allocate string.");
2865 log_debug("%s: got %s", u->meta.id, e);
2867 free(s->status_text);
2870 free(s->status_text);
2871 s->status_text = NULL;
2876 /* Notify clients about changed status or main pid */
2877 unit_add_to_dbus_queue(u);
2880 #ifdef HAVE_SYSV_COMPAT
2881 static int service_enumerate(Manager *m) {
2885 char *path = NULL, *fpath = NULL, *name = NULL;
2886 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
2893 zero(runlevel_services);
2895 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2896 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2901 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2909 if (!(d = opendir(path))) {
2910 if (errno != ENOENT)
2911 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2916 while ((de = readdir(d))) {
2919 if (ignore_file(de->d_name))
2922 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2925 if (strlen(de->d_name) < 4)
2928 a = undecchar(de->d_name[1]);
2929 b = undecchar(de->d_name[2]);
2936 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2941 if (access(fpath, X_OK) < 0) {
2943 if (errno != ENOENT)
2944 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2950 if (!(name = sysv_translate_name(de->d_name + 3))) {
2955 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2956 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2960 if (de->d_name[0] == 'S') {
2962 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
2963 SERVICE(service)->sysv_start_priority =
2964 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2966 SERVICE(service)->sysv_enabled = true;
2969 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
2972 if ((r = set_put(runlevel_services[i], service)) < 0)
2975 } else if (de->d_name[0] == 'K' &&
2976 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2977 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2979 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
2982 if ((r = set_put(shutdown_services, service)) < 0)
2988 /* Now we loaded all stubs and are aware of the lowest
2989 start-up priority for all services, not let's actually load
2990 the services, this will also tell us which services are
2991 actually native now */
2992 manager_dispatch_load_queue(m);
2994 /* If this is a native service, rely on native ways to pull in
2995 * a service, don't pull it in via sysv rcN.d links. */
2996 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
2997 SET_FOREACH(service, runlevel_services[i], j) {
2998 service = unit_follow_merge(service);
3000 if (service->meta.fragment_path)
3003 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3007 /* We honour K links only for halt/reboot. For the normal
3008 * runlevels we assume the stop jobs will be implicitly added
3009 * by the core logic. Also, we don't really distuingish here
3010 * between the runlevels 0 and 6 and just add them to the
3011 * special shutdown target. On SUSE the boot.d/ runlevel is
3012 * also used for shutdown, so we add links for that too to the
3013 * shutdown target.*/
3014 SET_FOREACH(service, shutdown_services, j) {
3015 service = unit_follow_merge(service);
3017 if (service->meta.fragment_path)
3020 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3031 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3032 set_free(runlevel_services[i]);
3033 set_free(shutdown_services);
3042 static void service_bus_name_owner_change(
3045 const char *old_owner,
3046 const char *new_owner) {
3048 Service *s = SERVICE(u);
3053 assert(streq(s->bus_name, name));
3054 assert(old_owner || new_owner);
3056 if (old_owner && new_owner)
3057 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
3059 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
3061 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
3063 s->bus_name_good = !!new_owner;
3065 if (s->type == SERVICE_DBUS) {
3067 /* service_enter_running() will figure out what to
3069 if (s->state == SERVICE_RUNNING)
3070 service_enter_running(s, true);
3071 else if (s->state == SERVICE_START && new_owner)
3072 service_enter_start_post(s);
3074 } else if (new_owner &&
3076 (s->state == SERVICE_START ||
3077 s->state == SERVICE_START_POST ||
3078 s->state == SERVICE_RUNNING ||
3079 s->state == SERVICE_RELOAD)) {
3081 /* Try to acquire PID from bus service */
3082 log_debug("Trying to acquire PID from D-Bus name...");
3084 bus_query_pid(u->meta.manager, name);
3088 static void service_bus_query_pid_done(
3093 Service *s = SERVICE(u);
3098 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
3100 if (s->main_pid <= 0 &&
3101 (s->state == SERVICE_START ||
3102 s->state == SERVICE_START_POST ||
3103 s->state == SERVICE_RUNNING ||
3104 s->state == SERVICE_RELOAD))
3105 service_set_main_pid(s, pid);
3108 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3112 /* This is called by the socket code when instantiating a new
3113 * service for a stream socket and the socket needs to be
3116 if (s->meta.load_state != UNIT_LOADED)
3119 if (s->socket_fd >= 0)
3122 if (s->state != SERVICE_DEAD)
3126 s->got_socket_fd = true;
3127 s->accept_socket = sock;
3132 static void service_reset_failed(Unit *u) {
3133 Service *s = SERVICE(u);
3137 if (s->state == SERVICE_FAILED)
3138 service_set_state(s, SERVICE_DEAD);
3143 static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
3144 Service *s = SERVICE(u);
3146 Set *pid_set = NULL;
3150 if (s->main_pid <= 0 && who == KILL_MAIN) {
3151 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3155 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3156 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3160 if (s->control_pid > 0)
3161 if (kill(mode == KILL_PROCESS_GROUP ? -s->control_pid : s->control_pid, signo) < 0)
3164 if (s->main_pid > 0)
3165 if (kill(mode == KILL_PROCESS_GROUP ? -s->main_pid : s->main_pid, signo) < 0)
3168 if (mode == KILL_CONTROL_GROUP) {
3171 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
3174 /* Exclude the control/main pid from being killed via the cgroup */
3175 if (s->control_pid > 0)
3176 if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
3181 if (s->main_pid > 0)
3182 if ((q = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) {
3187 if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, pid_set)) < 0)
3188 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
3199 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3200 [SERVICE_DEAD] = "dead",
3201 [SERVICE_START_PRE] = "start-pre",
3202 [SERVICE_START] = "start",
3203 [SERVICE_START_POST] = "start-post",
3204 [SERVICE_RUNNING] = "running",
3205 [SERVICE_EXITED] = "exited",
3206 [SERVICE_RELOAD] = "reload",
3207 [SERVICE_STOP] = "stop",
3208 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3209 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3210 [SERVICE_STOP_POST] = "stop-post",
3211 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3212 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3213 [SERVICE_FAILED] = "failed",
3214 [SERVICE_AUTO_RESTART] = "auto-restart",
3217 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3219 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3220 [SERVICE_RESTART_NO] = "no",
3221 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3222 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3223 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3224 [SERVICE_RESTART_ALWAYS] = "always"
3227 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3229 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3230 [SERVICE_SIMPLE] = "simple",
3231 [SERVICE_FORKING] = "forking",
3232 [SERVICE_ONESHOT] = "oneshot",
3233 [SERVICE_DBUS] = "dbus",
3234 [SERVICE_NOTIFY] = "notify"
3237 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3239 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3240 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3241 [SERVICE_EXEC_START] = "ExecStart",
3242 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3243 [SERVICE_EXEC_RELOAD] = "ExecReload",
3244 [SERVICE_EXEC_STOP] = "ExecStop",
3245 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3248 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3250 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3251 [NOTIFY_NONE] = "none",
3252 [NOTIFY_MAIN] = "main",
3253 [NOTIFY_ALL] = "all"
3256 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3258 const UnitVTable service_vtable = {
3259 .suffix = ".service",
3260 .show_status = true,
3262 .init = service_init,
3263 .done = service_done,
3264 .load = service_load,
3266 .coldplug = service_coldplug,
3268 .dump = service_dump,
3270 .start = service_start,
3271 .stop = service_stop,
3272 .reload = service_reload,
3274 .can_reload = service_can_reload,
3276 .kill = service_kill,
3278 .serialize = service_serialize,
3279 .deserialize_item = service_deserialize_item,
3281 .active_state = service_active_state,
3282 .sub_state_to_string = service_sub_state_to_string,
3284 #ifdef HAVE_SYSV_COMPAT
3285 .check_gc = service_check_gc,
3287 .check_snapshot = service_check_snapshot,
3289 .sigchld_event = service_sigchld_event,
3290 .timer_event = service_timer_event,
3292 .reset_failed = service_reset_failed,
3294 .cgroup_notify_empty = service_cgroup_notify_event,
3295 .notify_message = service_notify_message,
3297 .bus_name_owner_change = service_bus_name_owner_change,
3298 .bus_query_pid_done = service_bus_query_pid_done,
3300 .bus_interface = "org.freedesktop.systemd1.Service",
3301 .bus_message_handler = bus_service_message_handler,
3302 .bus_invalidating_properties = bus_service_invalidating_properties,
3304 #ifdef HAVE_SYSV_COMPAT
3305 .enumerate = service_enumerate