1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
36 #include "bus-errors.h"
38 #define COMMENTS "#;\n"
39 #define NEWLINES "\n\r"
41 #ifdef HAVE_SYSV_COMPAT
42 typedef enum RunlevelType {
51 const RunlevelType type;
53 /* Standard SysV runlevels for start-up */
54 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
55 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
56 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
57 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
58 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
61 /* SUSE style boot.d */
62 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
66 /* Debian style rcS.d */
67 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
70 /* Standard SysV runlevels for shutdown */
71 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
72 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
74 /* Note that the order here matters, as we read the
75 directories in this order, and we want to make sure that
76 sysv_start_priority is known when we first load the
77 unit. And that value we only know from S links. Hence
78 UP/SYSINIT must be read before DOWN */
81 #define RUNLEVELS_UP "12345"
82 /* #define RUNLEVELS_DOWN "06" */
83 /* #define RUNLEVELS_BOOT "bBsS" */
86 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
87 [SERVICE_DEAD] = UNIT_INACTIVE,
88 [SERVICE_START_PRE] = UNIT_ACTIVATING,
89 [SERVICE_START] = UNIT_ACTIVATING,
90 [SERVICE_START_POST] = UNIT_ACTIVATING,
91 [SERVICE_RUNNING] = UNIT_ACTIVE,
92 [SERVICE_EXITED] = UNIT_ACTIVE,
93 [SERVICE_RELOAD] = UNIT_RELOADING,
94 [SERVICE_STOP] = UNIT_DEACTIVATING,
95 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
96 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
97 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
98 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
99 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
100 [SERVICE_FAILED] = UNIT_FAILED,
101 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
104 static void service_init(Unit *u) {
105 Service *s = SERVICE(u);
108 assert(u->meta.load_state == UNIT_STUB);
110 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
111 s->restart_usec = DEFAULT_RESTART_USEC;
112 s->timer_watch.type = WATCH_INVALID;
113 #ifdef HAVE_SYSV_COMPAT
114 s->sysv_start_priority = -1;
118 exec_context_init(&s->exec_context);
120 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
122 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
125 static void service_unwatch_control_pid(Service *s) {
128 if (s->control_pid <= 0)
131 unit_unwatch_pid(UNIT(s), s->control_pid);
135 static void service_unwatch_main_pid(Service *s) {
138 if (s->main_pid <= 0)
141 unit_unwatch_pid(UNIT(s), s->main_pid);
145 static int service_set_main_pid(Service *s, pid_t pid) {
156 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
157 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
158 s->meta.id, (unsigned long) pid);
161 s->main_pid_known = true;
163 exec_status_start(&s->main_exec_status, pid);
168 static void service_close_socket_fd(Service *s) {
171 if (s->socket_fd < 0)
174 close_nointr_nofail(s->socket_fd);
178 static void service_connection_unref(Service *s) {
181 if (!s->accept_socket)
184 socket_connection_unref(s->accept_socket);
185 s->accept_socket = NULL;
188 static void service_done(Unit *u) {
189 Service *s = SERVICE(u);
196 #ifdef HAVE_SYSV_COMPAT
200 free(s->sysv_runlevels);
201 s->sysv_runlevels = NULL;
204 free(s->status_text);
205 s->status_text = NULL;
207 exec_context_done(&s->exec_context);
208 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
209 s->control_command = NULL;
211 /* This will leak a process, but at least no memory or any of
213 service_unwatch_main_pid(s);
214 service_unwatch_control_pid(s);
217 unit_unwatch_bus_name(UNIT(u), s->bus_name);
222 service_close_socket_fd(s);
223 service_connection_unref(s);
225 set_free(s->configured_sockets);
227 unit_unwatch_timer(u, &s->timer_watch);
230 #ifdef HAVE_SYSV_COMPAT
231 static char *sysv_translate_name(const char *name) {
234 if (!(r = new(char, strlen(name) + sizeof(".service"))))
237 if (startswith(name, "boot."))
238 /* Drop SuSE-style boot. prefix */
239 strcpy(stpcpy(r, name + 5), ".service");
240 else if (endswith(name, ".sh"))
241 /* Drop Debian-style .sh suffix */
242 strcpy(stpcpy(r, name) - 3, ".service");
244 else if (startswith(name, "@"))
245 /* Drop Arch-style background prefix */
246 strcpy(stpcpy(r, name + 1), ".service");
249 /* Normal init scripts */
250 strcpy(stpcpy(r, name), ".service");
255 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
257 /* We silently ignore the $ prefix here. According to the LSB
258 * spec it simply indicates whether something is a
259 * standardized name or a distribution-specific one. Since we
260 * just follow what already exists and do not introduce new
261 * uses or names we don't care who introduced a new name. */
263 static const char * const table[] = {
264 /* LSB defined facilities */
265 "local_fs", SPECIAL_LOCAL_FS_TARGET,
266 "network", SPECIAL_NETWORK_TARGET,
267 "named", SPECIAL_NSS_LOOKUP_TARGET,
268 "portmap", SPECIAL_RPCBIND_TARGET,
269 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
270 "syslog", SPECIAL_SYSLOG_TARGET,
271 "time", SPECIAL_RTC_SET_TARGET,
273 /* Debian extensions */
275 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
277 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
278 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
281 /* Fedora extensions */
282 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
283 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
284 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
287 /* SuSE extensions */
299 n = *name == '$' ? name + 1 : name;
301 for (i = 0; i < ELEMENTSOF(table); i += 2) {
303 if (!streq(table[i], n))
309 if (!(r = strdup(table[i+1])))
315 /* If we don't know this name, fallback heuristics to figure
316 * out whether something is a target or an service alias. */
319 /* Facilities starting with $ are most likely targets */
320 r = unit_name_build(n, NULL, ".target");
321 else if (filename && streq(name, filename))
322 /* Names equalling the file name of the services are redundant */
325 /* Everything else we assume to be normal service names */
326 r = sysv_translate_name(n);
339 static int sysv_fix_order(Service *s) {
345 if (s->sysv_start_priority < 0)
348 /* For each pair of services where at least one lacks a LSB
349 * header, we use the start priority value to order things. */
351 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
354 bool special_s, special_t;
356 t = (Service*) other;
361 if (t->meta.load_state != UNIT_LOADED)
364 if (t->sysv_start_priority < 0)
367 /* If both units have modern headers we don't care
368 * about the priorities */
369 if ((s->meta.fragment_path || s->sysv_has_lsb) &&
370 (t->meta.fragment_path || t->sysv_has_lsb))
373 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
374 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
376 if (special_t && !special_s)
378 else if (special_s && !special_t)
380 else if (t->sysv_start_priority < s->sysv_start_priority)
382 else if (t->sysv_start_priority > s->sysv_start_priority)
387 /* FIXME: Maybe we should compare the name here lexicographically? */
389 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
396 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
399 if (!(c = new0(ExecCommand, 1)))
402 if (!(c->path = strdup(path))) {
407 if (!(c->argv = strv_new(path, arg1, NULL))) {
416 static int sysv_exec_commands(Service *s) {
420 assert(s->sysv_path);
422 if (!(c = exec_command_new(s->sysv_path, "start")))
424 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
426 if (!(c = exec_command_new(s->sysv_path, "stop")))
428 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
430 if (!(c = exec_command_new(s->sysv_path, "reload")))
432 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
437 static int service_load_sysv_path(Service *s, const char *path) {
448 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
455 if (!(f = fopen(path, "re"))) {
456 r = errno == ENOENT ? 0 : -errno;
461 if (!(s->sysv_path = strdup(path))) {
467 char l[LINE_MAX], *t;
469 if (!fgets(l, sizeof(l), f)) {
474 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
484 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
486 s->sysv_has_lsb = true;
490 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
496 t += strspn(t, WHITESPACE);
498 if (state == NORMAL) {
500 /* Try to parse Red Hat style chkconfig headers */
502 if (startswith_no_case(t, "chkconfig:")) {
504 char runlevels[16], *k;
508 if (sscanf(t+10, "%15s %i %*i",
510 &start_priority) != 2) {
512 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
516 /* A start priority gathered from the
517 * symlink farms is preferred over the
518 * data from the LSB header. */
519 if (start_priority < 0 || start_priority > 99)
520 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
521 else if (s->sysv_start_priority < 0)
522 s->sysv_start_priority = start_priority;
524 char_array_0(runlevels);
525 k = delete_chars(runlevels, WHITESPACE "-");
530 if (!(d = strdup(k))) {
535 free(s->sysv_runlevels);
536 s->sysv_runlevels = d;
539 } else if (startswith_no_case(t, "description:")) {
541 size_t k = strlen(t);
545 if (t[k-1] == '\\') {
550 if ((j = strstrip(t+12)) && *j) {
551 if (!(d = strdup(j))) {
558 free(chkconfig_description);
559 chkconfig_description = d;
561 } else if (startswith_no_case(t, "pidfile:")) {
568 if (!path_is_absolute(fn)) {
569 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
573 if (!(fn = strdup(fn))) {
582 } else if (state == DESCRIPTION) {
584 /* Try to parse Red Hat style description
587 size_t k = strlen(t);
595 if ((j = strstrip(t)) && *j) {
598 if (chkconfig_description)
599 asprintf(&d, "%s %s", chkconfig_description, j);
608 free(chkconfig_description);
609 chkconfig_description = d;
612 } else if (state == LSB || state == LSB_DESCRIPTION) {
614 if (startswith_no_case(t, "Provides:")) {
620 FOREACH_WORD_QUOTED(w, z, t+9, i) {
623 if (!(n = strndup(w, z))) {
628 r = sysv_translate_facility(n, file_name_from_path(path), &m);
637 if (unit_name_to_type(m) == UNIT_SERVICE)
638 r = unit_add_name(u, m);
640 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
642 if (s->sysv_enabled) {
645 if ((k = unit_add_dependency_by_name_inverse(u, UNIT_WANTS, m, NULL, true)) < 0)
651 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
656 } else if (startswith_no_case(t, "Required-Start:") ||
657 startswith_no_case(t, "Should-Start:") ||
658 startswith_no_case(t, "X-Start-Before:") ||
659 startswith_no_case(t, "X-Start-After:")) {
665 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
668 if (!(n = strndup(w, z))) {
673 r = sysv_translate_facility(n, file_name_from_path(path), &m);
682 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
685 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
689 } else if (startswith_no_case(t, "Default-Start:")) {
694 k = delete_chars(t+14, WHITESPACE "-");
697 if (!(d = strdup(k))) {
702 free(s->sysv_runlevels);
703 s->sysv_runlevels = d;
706 } else if (startswith_no_case(t, "Description:")) {
709 state = LSB_DESCRIPTION;
711 if ((j = strstrip(t+12)) && *j) {
712 if (!(d = strdup(j))) {
719 free(long_description);
720 long_description = d;
722 } else if (startswith_no_case(t, "Short-Description:")) {
727 if ((j = strstrip(t+18)) && *j) {
728 if (!(d = strdup(j))) {
735 free(short_description);
736 short_description = d;
738 } else if (startswith_no_case(t, "X-Interactive:")) {
741 if ((b = parse_boolean(strstrip(t+14))) < 0) {
742 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
747 s->exec_context.std_input = EXEC_INPUT_TTY;
749 s->exec_context.std_input = EXEC_INPUT_NULL;
751 } else if (state == LSB_DESCRIPTION) {
753 if (startswith(l, "#\t") || startswith(l, "# ")) {
756 if ((j = strstrip(t)) && *j) {
759 if (long_description)
760 asprintf(&d, "%s %s", long_description, t);
769 free(long_description);
770 long_description = d;
779 if ((r = sysv_exec_commands(s)) < 0)
782 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
783 /* If there a runlevels configured for this service
784 * but none of the standard ones, then we assume this
785 * is some special kind of service (which might be
786 * needed for early boot) and don't create any links
789 s->meta.default_dependencies = false;
791 /* Don't timeout special services during boot (like fsck) */
795 /* Special setting for all SysV services */
796 s->type = SERVICE_FORKING;
797 s->remain_after_exit = true;
798 s->restart = SERVICE_RESTART_NO;
799 s->exec_context.std_output =
800 (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY)
801 ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
802 s->exec_context.kill_mode = KILL_PROCESS_GROUP;
804 /* We use the long description only if
805 * no short description is set. */
807 if (short_description)
808 description = short_description;
809 else if (chkconfig_description)
810 description = chkconfig_description;
811 else if (long_description)
812 description = long_description;
819 if (!(d = strappend("LSB: ", description))) {
824 u->meta.description = d;
827 u->meta.load_state = UNIT_LOADED;
835 free(short_description);
836 free(long_description);
837 free(chkconfig_description);
842 static int service_load_sysv_name(Service *s, const char *name) {
848 /* For SysV services we strip the boot. or .sh
849 * prefixes/suffixes. */
850 if (startswith(name, "boot.") ||
851 endswith(name, ".sh.service"))
854 STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
858 if (asprintf(&path, "%s/%s", *p, name) < 0)
861 assert(endswith(path, ".service"));
862 path[strlen(path)-8] = 0;
864 r = service_load_sysv_path(s, path);
866 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
867 /* Try Debian style xxx.sh source'able init scripts */
869 r = service_load_sysv_path(s, path);
874 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
875 /* Try SUSE style boot.xxx init scripts */
877 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
880 path[strlen(path)-8] = 0;
881 r = service_load_sysv_path(s, path);
888 if ((s->meta.load_state != UNIT_STUB))
895 static int service_load_sysv(Service *s) {
902 /* Load service data from SysV init scripts, preferably with
905 if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
908 if ((t = s->meta.id))
909 if ((r = service_load_sysv_name(s, t)) < 0)
912 if (s->meta.load_state == UNIT_STUB)
913 SET_FOREACH(t, s->meta.names, i) {
917 if ((r = service_load_sysv_name(s, t)) < 0)
920 if (s->meta.load_state != UNIT_STUB)
928 static int service_verify(Service *s) {
931 if (s->meta.load_state != UNIT_LOADED)
934 if (!s->exec_command[SERVICE_EXEC_START]) {
935 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
939 if (s->type != SERVICE_ONESHOT &&
940 s->exec_command[SERVICE_EXEC_START]->command_next) {
941 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
945 if (s->type == SERVICE_DBUS && !s->bus_name) {
946 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
950 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
951 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
958 static int service_add_default_dependencies(Service *s) {
963 /* Add a number of automatic dependencies useful for the
964 * majority of services. */
966 /* First, pull in base system */
967 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
969 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
972 } else if (s->meta.manager->running_as == MANAGER_SESSION) {
974 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
978 /* Second, activate normal shutdown */
979 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
982 static int service_load(Unit *u) {
984 Service *s = SERVICE(u);
988 /* Load a .service file */
989 if ((r = unit_load_fragment(u)) < 0)
992 #ifdef HAVE_SYSV_COMPAT
993 /* Load a classic init script as a fallback, if we couldn't find anything */
994 if (u->meta.load_state == UNIT_STUB)
995 if ((r = service_load_sysv(s)) < 0)
999 /* Still nothing found? Then let's give up */
1000 if (u->meta.load_state == UNIT_STUB)
1003 /* We were able to load something, then let's add in the
1004 * dropin directories. */
1005 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1008 /* This is a new unit? Then let's add in some extras */
1009 if (u->meta.load_state == UNIT_LOADED) {
1010 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1013 if ((r = unit_add_default_cgroup(u)) < 0)
1016 #ifdef HAVE_SYSV_COMPAT
1017 if ((r = sysv_fix_order(s)) < 0)
1022 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1025 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1026 s->notify_access = NOTIFY_MAIN;
1028 if (s->type == SERVICE_DBUS || s->bus_name)
1029 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
1032 if (s->meta.default_dependencies)
1033 if ((r = service_add_default_dependencies(s)) < 0)
1037 return service_verify(s);
1040 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1042 ServiceExecCommand c;
1043 Service *s = SERVICE(u);
1044 const char *prefix2;
1049 p2 = strappend(prefix, "\t");
1050 prefix2 = p2 ? p2 : prefix;
1053 "%sService State: %s\n"
1054 "%sPermissionsStartOnly: %s\n"
1055 "%sRootDirectoryStartOnly: %s\n"
1056 "%sRemainAfterExit: %s\n"
1059 "%sNotifyAccess: %s\n",
1060 prefix, service_state_to_string(s->state),
1061 prefix, yes_no(s->permissions_start_only),
1062 prefix, yes_no(s->root_directory_start_only),
1063 prefix, yes_no(s->remain_after_exit),
1064 prefix, service_type_to_string(s->type),
1065 prefix, service_restart_to_string(s->restart),
1066 prefix, notify_access_to_string(s->notify_access));
1068 if (s->control_pid > 0)
1070 "%sControl PID: %lu\n",
1071 prefix, (unsigned long) s->control_pid);
1073 if (s->main_pid > 0)
1075 "%sMain PID: %lu\n",
1076 prefix, (unsigned long) s->main_pid);
1081 prefix, s->pid_file);
1086 "%sBus Name Good: %s\n",
1087 prefix, s->bus_name,
1088 prefix, yes_no(s->bus_name_good));
1090 exec_context_dump(&s->exec_context, f, prefix);
1092 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1094 if (!s->exec_command[c])
1097 fprintf(f, "%s-> %s:\n",
1098 prefix, service_exec_command_to_string(c));
1100 exec_command_dump_list(s->exec_command[c], f, prefix2);
1103 #ifdef HAVE_SYSV_COMPAT
1106 "%sSysV Init Script Path: %s\n"
1107 "%sSysV Init Script has LSB Header: %s\n",
1108 prefix, s->sysv_path,
1109 prefix, yes_no(s->sysv_has_lsb));
1111 if (s->sysv_start_priority >= 0)
1113 "%sSysVStartPriority: %i\n"
1114 "%sSysVEnabled: %s\n",
1115 prefix, s->sysv_start_priority,
1116 prefix, yes_no(s->sysv_enabled));
1118 if (s->sysv_runlevels)
1119 fprintf(f, "%sSysVRunLevels: %s\n",
1120 prefix, s->sysv_runlevels);
1124 fprintf(f, "%sStatus Text: %s\n",
1125 prefix, s->status_text);
1130 static int service_load_pid_file(Service *s) {
1137 if (s->main_pid_known)
1140 assert(s->main_pid <= 0);
1145 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1148 r = parse_pid(k, &pid);
1154 if (kill(pid, 0) < 0 && errno != EPERM) {
1155 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1156 (unsigned long) pid, s->pid_file);
1160 if ((r = service_set_main_pid(s, pid)) < 0)
1163 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1164 /* FIXME: we need to do something here */
1170 static int service_get_sockets(Service *s, Set **_set) {
1179 if (s->socket_fd >= 0)
1182 if (!set_isempty(s->configured_sockets))
1185 /* Collects all Socket objects that belong to this
1186 * service. Note that a service might have multiple sockets
1187 * via multiple names. */
1189 if (!(set = set_new(NULL, NULL)))
1192 SET_FOREACH(t, s->meta.names, i) {
1196 /* Look for all socket objects that go by any of our
1197 * units and collect their fds */
1199 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1204 p = manager_get_unit(s->meta.manager, k);
1210 if ((r = set_put(set, p)) < 0)
1222 static int service_notify_sockets_dead(Service *s) {
1224 Set *set, *free_set = NULL;
1230 /* Notifies all our sockets when we die */
1232 if (s->socket_fd >= 0)
1235 if (!set_isempty(s->configured_sockets))
1236 set = s->configured_sockets;
1238 if ((r = service_get_sockets(s, &free_set)) < 0)
1244 SET_FOREACH(sock, set, i)
1245 socket_notify_service_dead(sock);
1252 static void service_set_state(Service *s, ServiceState state) {
1253 ServiceState old_state;
1256 old_state = s->state;
1259 if (state != SERVICE_START_PRE &&
1260 state != SERVICE_START &&
1261 state != SERVICE_START_POST &&
1262 state != SERVICE_RELOAD &&
1263 state != SERVICE_STOP &&
1264 state != SERVICE_STOP_SIGTERM &&
1265 state != SERVICE_STOP_SIGKILL &&
1266 state != SERVICE_STOP_POST &&
1267 state != SERVICE_FINAL_SIGTERM &&
1268 state != SERVICE_FINAL_SIGKILL &&
1269 state != SERVICE_AUTO_RESTART)
1270 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1272 if (state != SERVICE_START &&
1273 state != SERVICE_START_POST &&
1274 state != SERVICE_RUNNING &&
1275 state != SERVICE_RELOAD &&
1276 state != SERVICE_STOP &&
1277 state != SERVICE_STOP_SIGTERM &&
1278 state != SERVICE_STOP_SIGKILL)
1279 service_unwatch_main_pid(s);
1281 if (state != SERVICE_START_PRE &&
1282 state != SERVICE_START &&
1283 state != SERVICE_START_POST &&
1284 state != SERVICE_RELOAD &&
1285 state != SERVICE_STOP &&
1286 state != SERVICE_STOP_SIGTERM &&
1287 state != SERVICE_STOP_SIGKILL &&
1288 state != SERVICE_STOP_POST &&
1289 state != SERVICE_FINAL_SIGTERM &&
1290 state != SERVICE_FINAL_SIGKILL) {
1291 service_unwatch_control_pid(s);
1292 s->control_command = NULL;
1293 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1296 if (state == SERVICE_DEAD ||
1297 state == SERVICE_STOP ||
1298 state == SERVICE_STOP_SIGTERM ||
1299 state == SERVICE_STOP_SIGKILL ||
1300 state == SERVICE_STOP_POST ||
1301 state == SERVICE_FINAL_SIGTERM ||
1302 state == SERVICE_FINAL_SIGKILL ||
1303 state == SERVICE_FAILED ||
1304 state == SERVICE_AUTO_RESTART)
1305 service_notify_sockets_dead(s);
1307 if (state != SERVICE_START_PRE &&
1308 state != SERVICE_START &&
1309 state != SERVICE_START_POST &&
1310 state != SERVICE_RUNNING &&
1311 state != SERVICE_RELOAD &&
1312 state != SERVICE_STOP &&
1313 state != SERVICE_STOP_SIGTERM &&
1314 state != SERVICE_STOP_SIGKILL &&
1315 state != SERVICE_STOP_POST &&
1316 state != SERVICE_FINAL_SIGTERM &&
1317 state != SERVICE_FINAL_SIGKILL &&
1318 !(state == SERVICE_DEAD && s->meta.job)) {
1319 service_close_socket_fd(s);
1320 service_connection_unref(s);
1323 /* For the inactive states unit_notify() will trim the cgroup,
1324 * but for exit we have to do that ourselves... */
1325 if (state == SERVICE_EXITED)
1326 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1328 if (old_state != state)
1329 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1331 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1334 static int service_coldplug(Unit *u) {
1335 Service *s = SERVICE(u);
1339 assert(s->state == SERVICE_DEAD);
1341 if (s->deserialized_state != s->state) {
1343 if (s->deserialized_state == SERVICE_START_PRE ||
1344 s->deserialized_state == SERVICE_START ||
1345 s->deserialized_state == SERVICE_START_POST ||
1346 s->deserialized_state == SERVICE_RELOAD ||
1347 s->deserialized_state == SERVICE_STOP ||
1348 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1349 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1350 s->deserialized_state == SERVICE_STOP_POST ||
1351 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1352 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1353 s->deserialized_state == SERVICE_AUTO_RESTART) {
1355 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1358 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1360 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1365 if ((s->deserialized_state == SERVICE_START &&
1366 (s->type == SERVICE_FORKING ||
1367 s->type == SERVICE_DBUS ||
1368 s->type == SERVICE_ONESHOT ||
1369 s->type == SERVICE_NOTIFY)) ||
1370 s->deserialized_state == SERVICE_START_POST ||
1371 s->deserialized_state == SERVICE_RUNNING ||
1372 s->deserialized_state == SERVICE_RELOAD ||
1373 s->deserialized_state == SERVICE_STOP ||
1374 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1375 s->deserialized_state == SERVICE_STOP_SIGKILL)
1376 if (s->main_pid > 0)
1377 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1380 if (s->deserialized_state == SERVICE_START_PRE ||
1381 s->deserialized_state == SERVICE_START ||
1382 s->deserialized_state == SERVICE_START_POST ||
1383 s->deserialized_state == SERVICE_RELOAD ||
1384 s->deserialized_state == SERVICE_STOP ||
1385 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1386 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1387 s->deserialized_state == SERVICE_STOP_POST ||
1388 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1389 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1390 if (s->control_pid > 0)
1391 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1394 service_set_state(s, s->deserialized_state);
1400 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1404 unsigned rn_fds = 0;
1405 Set *set, *free_set = NULL;
1412 if (s->socket_fd >= 0)
1415 if (!set_isempty(s->configured_sockets))
1416 set = s->configured_sockets;
1418 if ((r = service_get_sockets(s, &free_set)) < 0)
1424 SET_FOREACH(sock, set, i) {
1428 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1440 if (!(t = new(int, rn_fds+cn_fds))) {
1446 memcpy(t, rfds, rn_fds);
1447 memcpy(t+rn_fds, cfds, cn_fds);
1452 rn_fds = rn_fds+cn_fds;
1470 static int service_spawn(
1475 bool apply_permissions,
1477 bool apply_tty_stdin,
1478 bool set_notify_socket,
1483 int *fds = NULL, *fdsbuf = NULL;
1484 unsigned n_fds = 0, n_env = 0;
1485 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1492 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1493 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1494 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1496 if (s->socket_fd >= 0) {
1497 fds = &s->socket_fd;
1500 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1507 if (timeout && s->timeout_usec) {
1508 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1511 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1513 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1518 if (!(our_env = new0(char*, 3))) {
1523 if (set_notify_socket)
1524 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1529 if (s->main_pid > 0)
1530 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1535 if (!(final_env = strv_env_merge(2,
1536 s->meta.manager->environment,
1551 s->meta.manager->confirm_spawn,
1552 s->meta.cgroup_bondings,
1559 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1560 /* FIXME: we need to do something here */
1566 strv_free(final_env);
1576 strv_free(final_env);
1579 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1584 static int main_pid_good(Service *s) {
1587 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1590 /* If we know the pid file, then lets just check if it is
1592 if (s->main_pid_known)
1593 return s->main_pid > 0;
1595 /* We don't know the pid */
1599 static int control_pid_good(Service *s) {
1602 return s->control_pid > 0;
1605 static int cgroup_good(Service *s) {
1610 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1616 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1623 if (allow_restart &&
1624 !s->forbid_restart &&
1625 (s->restart == SERVICE_RESTART_ALWAYS ||
1626 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1628 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1631 service_set_state(s, SERVICE_AUTO_RESTART);
1633 service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD);
1635 s->forbid_restart = false;
1640 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1641 service_enter_dead(s, false, false);
1644 static void service_enter_signal(Service *s, ServiceState state, bool success);
1646 static void service_enter_stop_post(Service *s, bool success) {
1653 service_unwatch_control_pid(s);
1655 s->control_command_id = SERVICE_EXEC_STOP_POST;
1656 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1657 if ((r = service_spawn(s,
1661 !s->permissions_start_only,
1662 !s->root_directory_start_only,
1665 &s->control_pid)) < 0)
1669 service_set_state(s, SERVICE_STOP_POST);
1671 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1676 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1677 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1680 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1682 Set *pid_set = NULL;
1683 bool wait_for_exit = false;
1690 if (s->exec_context.kill_mode != KILL_NONE) {
1691 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1693 if (s->main_pid > 0) {
1694 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1696 s->main_pid, sig) < 0 && errno != ESRCH)
1698 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1700 wait_for_exit = true;
1703 if (s->control_pid > 0) {
1704 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1706 s->control_pid, sig) < 0 && errno != ESRCH)
1708 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1710 wait_for_exit = true;
1713 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1715 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1720 /* Exclude the main/control pids from being killed via the cgroup */
1721 if (s->main_pid > 0)
1722 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1725 if (s->control_pid > 0)
1726 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1729 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
1730 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1731 log_warning("Failed to kill control group: %s", strerror(-r));
1733 wait_for_exit = true;
1739 if (wait_for_exit) {
1740 if (s->timeout_usec > 0)
1741 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1744 service_set_state(s, state);
1745 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1746 service_enter_stop_post(s, true);
1748 service_enter_dead(s, true, true);
1753 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1755 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1756 service_enter_stop_post(s, false);
1758 service_enter_dead(s, false, true);
1764 static void service_enter_stop(Service *s, bool success) {
1772 service_unwatch_control_pid(s);
1774 s->control_command_id = SERVICE_EXEC_STOP;
1775 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1776 if ((r = service_spawn(s,
1780 !s->permissions_start_only,
1781 !s->root_directory_start_only,
1784 &s->control_pid)) < 0)
1787 service_set_state(s, SERVICE_STOP);
1789 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1794 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1795 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1798 static void service_enter_running(Service *s, bool success) {
1799 int main_pid_ok, cgroup_ok;
1805 main_pid_ok = main_pid_good(s);
1806 cgroup_ok = cgroup_good(s);
1808 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1809 (s->bus_name_good || s->type != SERVICE_DBUS))
1810 service_set_state(s, SERVICE_RUNNING);
1811 else if (s->remain_after_exit)
1812 service_set_state(s, SERVICE_EXITED);
1814 service_enter_stop(s, true);
1817 static void service_enter_start_post(Service *s) {
1821 service_unwatch_control_pid(s);
1823 s->control_command_id = SERVICE_EXEC_START_POST;
1824 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1825 if ((r = service_spawn(s,
1829 !s->permissions_start_only,
1830 !s->root_directory_start_only,
1833 &s->control_pid)) < 0)
1836 service_set_state(s, SERVICE_START_POST);
1838 service_enter_running(s, true);
1843 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1844 service_enter_stop(s, false);
1847 static void service_enter_start(Service *s) {
1853 assert(s->exec_command[SERVICE_EXEC_START]);
1854 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1856 if (s->type == SERVICE_FORKING)
1857 service_unwatch_control_pid(s);
1859 service_unwatch_main_pid(s);
1861 s->control_command_id = SERVICE_EXEC_START;
1862 s->control_command = s->exec_command[SERVICE_EXEC_START];
1864 if ((r = service_spawn(s,
1866 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1871 s->notify_access != NOTIFY_NONE,
1875 if (s->type == SERVICE_SIMPLE) {
1876 /* For simple services we immediately start
1877 * the START_POST binaries. */
1879 service_set_main_pid(s, pid);
1880 service_enter_start_post(s);
1882 } else if (s->type == SERVICE_FORKING) {
1884 /* For forking services we wait until the start
1885 * process exited. */
1887 s->control_pid = pid;
1888 service_set_state(s, SERVICE_START);
1890 } else if (s->type == SERVICE_ONESHOT ||
1891 s->type == SERVICE_DBUS ||
1892 s->type == SERVICE_NOTIFY) {
1894 /* For oneshot services we wait until the start
1895 * process exited, too, but it is our main process. */
1897 /* For D-Bus services we know the main pid right away,
1898 * but wait for the bus name to appear on the
1899 * bus. Notify services are similar. */
1901 service_set_main_pid(s, pid);
1902 service_set_state(s, SERVICE_START);
1904 assert_not_reached("Unknown service type");
1909 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1910 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1913 static void service_enter_start_pre(Service *s) {
1918 service_unwatch_control_pid(s);
1920 s->control_command_id = SERVICE_EXEC_START_PRE;
1921 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1922 if ((r = service_spawn(s,
1926 !s->permissions_start_only,
1927 !s->root_directory_start_only,
1930 &s->control_pid)) < 0)
1933 service_set_state(s, SERVICE_START_PRE);
1935 service_enter_start(s);
1940 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1941 service_enter_dead(s, false, true);
1944 static void service_enter_restart(Service *s) {
1949 dbus_error_init(&error);
1951 service_enter_dead(s, true, false);
1953 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
1956 log_debug("%s scheduled restart job.", s->meta.id);
1960 log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
1961 service_enter_dead(s, false, false);
1963 dbus_error_free(&error);
1966 static void service_enter_reload(Service *s) {
1971 service_unwatch_control_pid(s);
1973 s->control_command_id = SERVICE_EXEC_RELOAD;
1974 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1975 if ((r = service_spawn(s,
1979 !s->permissions_start_only,
1980 !s->root_directory_start_only,
1983 &s->control_pid)) < 0)
1986 service_set_state(s, SERVICE_RELOAD);
1988 service_enter_running(s, true);
1993 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1994 service_enter_stop(s, false);
1997 static void service_run_next_control(Service *s, bool success) {
2001 assert(s->control_command);
2002 assert(s->control_command->command_next);
2007 assert(s->control_command_id != SERVICE_EXEC_START);
2009 s->control_command = s->control_command->command_next;
2010 service_unwatch_control_pid(s);
2012 if ((r = service_spawn(s,
2016 !s->permissions_start_only,
2017 !s->root_directory_start_only,
2018 s->control_command_id == SERVICE_EXEC_START_PRE ||
2019 s->control_command_id == SERVICE_EXEC_STOP_POST,
2021 &s->control_pid)) < 0)
2027 log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
2029 if (s->state == SERVICE_START_PRE)
2030 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2031 else if (s->state == SERVICE_STOP)
2032 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2033 else if (s->state == SERVICE_STOP_POST)
2034 service_enter_dead(s, false, true);
2036 service_enter_stop(s, false);
2039 static void service_run_next_main(Service *s, bool success) {
2044 assert(s->control_command);
2045 assert(s->control_command->command_next);
2050 assert(s->control_command_id == SERVICE_EXEC_START);
2051 assert(s->type == SERVICE_ONESHOT);
2053 s->control_command = s->control_command->command_next;
2054 service_unwatch_main_pid(s);
2056 if ((r = service_spawn(s,
2063 s->notify_access != NOTIFY_NONE,
2067 service_set_main_pid(s, pid);
2072 log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
2073 service_enter_stop(s, false);
2076 static int service_start(Unit *u) {
2077 Service *s = SERVICE(u);
2081 /* We cannot fulfill this request right now, try again later
2083 if (s->state == SERVICE_STOP ||
2084 s->state == SERVICE_STOP_SIGTERM ||
2085 s->state == SERVICE_STOP_SIGKILL ||
2086 s->state == SERVICE_STOP_POST ||
2087 s->state == SERVICE_FINAL_SIGTERM ||
2088 s->state == SERVICE_FINAL_SIGKILL)
2091 /* Already on it! */
2092 if (s->state == SERVICE_START_PRE ||
2093 s->state == SERVICE_START ||
2094 s->state == SERVICE_START_POST)
2097 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
2099 /* Make sure we don't enter a busy loop of some kind. */
2100 if (!ratelimit_test(&s->ratelimit)) {
2101 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
2106 s->main_pid_known = false;
2107 s->forbid_restart = false;
2109 service_enter_start_pre(s);
2113 static int service_stop(Unit *u) {
2114 Service *s = SERVICE(u);
2118 /* This is a user request, so don't do restarts on this
2120 s->forbid_restart = true;
2123 if (s->state == SERVICE_STOP ||
2124 s->state == SERVICE_STOP_SIGTERM ||
2125 s->state == SERVICE_STOP_SIGKILL ||
2126 s->state == SERVICE_STOP_POST ||
2127 s->state == SERVICE_FINAL_SIGTERM ||
2128 s->state == SERVICE_FINAL_SIGKILL)
2131 /* Don't allow a restart */
2132 if (s->state == SERVICE_AUTO_RESTART) {
2133 service_set_state(s, SERVICE_DEAD);
2137 /* If there's already something running we go directly into
2139 if (s->state == SERVICE_START_PRE ||
2140 s->state == SERVICE_START ||
2141 s->state == SERVICE_START_POST ||
2142 s->state == SERVICE_RELOAD) {
2143 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2147 assert(s->state == SERVICE_RUNNING ||
2148 s->state == SERVICE_EXITED);
2150 service_enter_stop(s, true);
2154 static int service_reload(Unit *u) {
2155 Service *s = SERVICE(u);
2159 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2161 service_enter_reload(s);
2165 static bool service_can_reload(Unit *u) {
2166 Service *s = SERVICE(u);
2170 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2173 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2174 Service *s = SERVICE(u);
2180 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2181 unit_serialize_item(u, f, "failure", yes_no(s->failure));
2183 if (s->control_pid > 0)
2184 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2186 if (s->main_pid_known && s->main_pid > 0)
2187 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2189 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2192 unit_serialize_item(u, f, "status-text", s->status_text);
2194 /* There's a minor uncleanliness here: if there are multiple
2195 * commands attached here, we will start from the first one
2197 if (s->control_command_id >= 0)
2198 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2200 if (s->socket_fd >= 0) {
2203 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2206 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2209 if (s->main_exec_status.pid > 0) {
2210 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2212 if (s->main_exec_status.start_timestamp.realtime > 0) {
2213 unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2214 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2216 unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2217 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2220 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2221 unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2222 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2223 unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2224 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2226 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2227 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2234 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2235 Service *s = SERVICE(u);
2242 if (streq(key, "state")) {
2245 if ((state = service_state_from_string(value)) < 0)
2246 log_debug("Failed to parse state value %s", value);
2248 s->deserialized_state = state;
2249 } else if (streq(key, "failure")) {
2252 if ((b = parse_boolean(value)) < 0)
2253 log_debug("Failed to parse failure value %s", value);
2255 s->failure = b || s->failure;
2256 } else if (streq(key, "control-pid")) {
2259 if (parse_pid(value, &pid) < 0)
2260 log_debug("Failed to parse control-pid value %s", value);
2262 s->control_pid = pid;
2263 } else if (streq(key, "main-pid")) {
2266 if (parse_pid(value, &pid) < 0)
2267 log_debug("Failed to parse main-pid value %s", value);
2269 service_set_main_pid(s, (pid_t) pid);
2270 } else if (streq(key, "main-pid-known")) {
2273 if ((b = parse_boolean(value)) < 0)
2274 log_debug("Failed to parse main-pid-known value %s", value);
2276 s->main_pid_known = b;
2277 } else if (streq(key, "status-text")) {
2280 if ((t = strdup(value))) {
2281 free(s->status_text);
2285 } else if (streq(key, "control-command")) {
2286 ServiceExecCommand id;
2288 if ((id = service_exec_command_from_string(value)) < 0)
2289 log_debug("Failed to parse exec-command value %s", value);
2291 s->control_command_id = id;
2292 s->control_command = s->exec_command[id];
2294 } else if (streq(key, "socket-fd")) {
2297 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2298 log_debug("Failed to parse socket-fd value %s", value);
2301 if (s->socket_fd >= 0)
2302 close_nointr_nofail(s->socket_fd);
2303 s->socket_fd = fdset_remove(fds, fd);
2305 } else if (streq(key, "main-exec-status-pid")) {
2308 if (parse_pid(value, &pid) < 0)
2309 log_debug("Failed to parse main-exec-status-pid value %s", value);
2311 s->main_exec_status.pid = pid;
2312 } else if (streq(key, "main-exec-status-code")) {
2315 if (safe_atoi(value, &i) < 0)
2316 log_debug("Failed to parse main-exec-status-code value %s", value);
2318 s->main_exec_status.code = i;
2319 } else if (streq(key, "main-exec-status-status")) {
2322 if (safe_atoi(value, &i) < 0)
2323 log_debug("Failed to parse main-exec-status-status value %s", value);
2325 s->main_exec_status.status = i;
2326 } else if (streq(key, "main-exec-status-start-realtime")) {
2329 if (safe_atou64(value, &k) < 0)
2330 log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2332 s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2333 } else if (streq(key, "main-exec-status-start-monotonic")) {
2336 if (safe_atou64(value, &k) < 0)
2337 log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2339 s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2340 } else if (streq(key, "main-exec-status-exit-realtime")) {
2343 if (safe_atou64(value, &k) < 0)
2344 log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2346 s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2347 } else if (streq(key, "main-exec-status-exit-monotonic")) {
2350 if (safe_atou64(value, &k) < 0)
2351 log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2353 s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2355 log_debug("Unknown serialization key '%s'", key);
2360 static UnitActiveState service_active_state(Unit *u) {
2363 return state_translation_table[SERVICE(u)->state];
2366 static const char *service_sub_state_to_string(Unit *u) {
2369 return service_state_to_string(SERVICE(u)->state);
2372 #ifdef HAVE_SYSV_COMPAT
2373 static bool service_check_gc(Unit *u) {
2374 Service *s = SERVICE(u);
2378 return !!s->sysv_path;
2382 static bool service_check_snapshot(Unit *u) {
2383 Service *s = SERVICE(u);
2387 return !s->got_socket_fd;
2390 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2391 Service *s = SERVICE(u);
2397 if (!s->meta.fragment_path)
2398 success = is_clean_exit_lsb(code, status);
2400 success = is_clean_exit(code, status);
2402 if (s->main_pid == pid) {
2405 exec_status_exit(&s->main_exec_status, pid, code, status);
2407 if (s->type != SERVICE_FORKING && s->control_command) {
2408 s->control_command->exec_status = s->main_exec_status;
2410 if (s->control_command->ignore)
2414 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2415 "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2416 s->failure = s->failure || !success;
2418 if (s->control_command &&
2419 s->control_command->command_next &&
2422 /* There is another command to *
2423 * execute, so let's do that. */
2425 log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2426 service_run_next_main(s, success);
2430 /* The service exited, so the service is officially
2433 s->control_command = NULL;
2434 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2438 case SERVICE_START_POST:
2439 case SERVICE_RELOAD:
2441 /* Need to wait until the operation is
2446 if (s->type == SERVICE_ONESHOT) {
2447 /* This was our main goal, so let's go on */
2449 service_enter_start_post(s);
2451 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2454 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2459 case SERVICE_RUNNING:
2460 service_enter_running(s, success);
2463 case SERVICE_STOP_SIGTERM:
2464 case SERVICE_STOP_SIGKILL:
2466 if (!control_pid_good(s))
2467 service_enter_stop_post(s, success);
2469 /* If there is still a control process, wait for that first */
2473 assert_not_reached("Uh, main process died at wrong time.");
2477 } else if (s->control_pid == pid) {
2481 if (s->control_command) {
2482 exec_status_exit(&s->control_command->exec_status, pid, code, status);
2484 if (s->control_command->ignore)
2488 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2489 "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2490 s->failure = s->failure || !success;
2492 if (s->control_command &&
2493 s->control_command->command_next &&
2496 /* There is another command to *
2497 * execute, so let's do that. */
2499 log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2500 service_run_next_control(s, success);
2503 /* No further commands for this step, so let's
2504 * figure out what to do next */
2506 s->control_command = NULL;
2507 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2509 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2513 case SERVICE_START_PRE:
2515 service_enter_start(s);
2517 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2521 assert(s->type == SERVICE_FORKING);
2523 /* Let's try to load the pid
2524 * file here if we can. We
2525 * ignore the return value,
2526 * since the PID file might
2527 * actually be created by a
2528 * START_POST script */
2532 service_load_pid_file(s);
2534 service_enter_start_post(s);
2536 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2540 case SERVICE_START_POST:
2541 if (success && s->pid_file && !s->main_pid_known) {
2544 /* Hmm, let's see if we can
2545 * load the pid now after the
2546 * start-post scripts got
2549 if ((r = service_load_pid_file(s)) < 0)
2550 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2555 case SERVICE_RELOAD:
2557 service_enter_running(s, true);
2559 service_enter_stop(s, false);
2564 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2567 case SERVICE_STOP_SIGTERM:
2568 case SERVICE_STOP_SIGKILL:
2569 if (main_pid_good(s) <= 0)
2570 service_enter_stop_post(s, success);
2572 /* If there is still a service
2573 * process around, wait until
2574 * that one quit, too */
2577 case SERVICE_STOP_POST:
2578 case SERVICE_FINAL_SIGTERM:
2579 case SERVICE_FINAL_SIGKILL:
2580 service_enter_dead(s, success, true);
2584 assert_not_reached("Uh, control process died at wrong time.");
2589 /* Notify clients about changed exit status */
2590 unit_add_to_dbus_queue(u);
2593 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2594 Service *s = SERVICE(u);
2597 assert(elapsed == 1);
2599 assert(w == &s->timer_watch);
2603 case SERVICE_START_PRE:
2605 log_warning("%s operation timed out. Terminating.", u->meta.id);
2606 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2609 case SERVICE_START_POST:
2610 case SERVICE_RELOAD:
2611 log_warning("%s operation timed out. Stopping.", u->meta.id);
2612 service_enter_stop(s, false);
2616 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2617 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2620 case SERVICE_STOP_SIGTERM:
2621 log_warning("%s stopping timed out. Killing.", u->meta.id);
2622 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2625 case SERVICE_STOP_SIGKILL:
2626 /* Uh, wie sent a SIGKILL and it is still not gone?
2627 * Must be something we cannot kill, so let's just be
2628 * weirded out and continue */
2630 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2631 service_enter_stop_post(s, false);
2634 case SERVICE_STOP_POST:
2635 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2636 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2639 case SERVICE_FINAL_SIGTERM:
2640 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2641 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2644 case SERVICE_FINAL_SIGKILL:
2645 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
2646 service_enter_dead(s, false, true);
2649 case SERVICE_AUTO_RESTART:
2650 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2651 service_enter_restart(s);
2655 assert_not_reached("Timeout at wrong time.");
2659 static void service_cgroup_notify_event(Unit *u) {
2660 Service *s = SERVICE(u);
2664 log_debug("%s: cgroup is empty", u->meta.id);
2668 /* Waiting for SIGCHLD is usually more interesting,
2669 * because it includes return codes/signals. Which is
2670 * why we ignore the cgroup events for most cases,
2671 * except when we don't know pid which to expect the
2674 case SERVICE_RUNNING:
2675 service_enter_running(s, true);
2678 case SERVICE_STOP_SIGTERM:
2679 case SERVICE_STOP_SIGKILL:
2680 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2681 service_enter_stop_post(s, true);
2685 case SERVICE_FINAL_SIGTERM:
2686 case SERVICE_FINAL_SIGKILL:
2687 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2688 service_enter_dead(s, true, true);
2697 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2698 Service *s = SERVICE(u);
2703 if (s->notify_access == NOTIFY_NONE) {
2704 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2705 u->meta.id, (unsigned long) pid);
2709 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2710 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2711 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2715 log_debug("%s: Got message", u->meta.id);
2717 /* Interpret MAINPID= */
2718 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2719 (s->state == SERVICE_START ||
2720 s->state == SERVICE_START_POST ||
2721 s->state == SERVICE_RUNNING ||
2722 s->state == SERVICE_RELOAD)) {
2724 if (parse_pid(e + 8, &pid) < 0)
2725 log_warning("Failed to parse notification message %s", e);
2727 log_debug("%s: got %s", u->meta.id, e);
2728 service_set_main_pid(s, pid);
2732 /* Interpret READY= */
2733 if (s->type == SERVICE_NOTIFY &&
2734 s->state == SERVICE_START &&
2735 strv_find(tags, "READY=1")) {
2736 log_debug("%s: got READY=1", u->meta.id);
2738 service_enter_start_post(s);
2741 /* Interpret STATUS= */
2742 if ((e = strv_find_prefix(tags, "STATUS="))) {
2746 if (!(t = strdup(e+7))) {
2747 log_error("Failed to allocate string.");
2751 log_debug("%s: got %s", u->meta.id, e);
2753 free(s->status_text);
2756 free(s->status_text);
2757 s->status_text = NULL;
2762 /* Notify clients about changed status or main pid */
2763 unit_add_to_dbus_queue(u);
2766 #ifdef HAVE_SYSV_COMPAT
2767 static int service_enumerate(Manager *m) {
2771 char *path = NULL, *fpath = NULL, *name = NULL;
2772 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
2777 Unit *previous = NULL;
2778 char *arch_daemons = NULL;
2779 char *arch_daemons_stripped = NULL;
2780 char **arch_daemons_split = NULL;
2786 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
2787 "DAEMONS", &arch_daemons,
2791 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
2793 } else if (arch_daemons) {
2794 if (!(arch_daemons_stripped = strchr(arch_daemons, '(')))
2795 arch_daemons_stripped = arch_daemons;
2797 arch_daemons_stripped++; /* strip start paren */
2799 arch_daemons_stripped[strcspn(arch_daemons_stripped, ")")] = 0; /* strip end paren */
2801 if (!(arch_daemons_split = strv_split_quoted(arch_daemons_stripped))) {
2806 STRV_FOREACH(p, arch_daemons_split) {
2811 if (**p == '!') /* daemons prefixed with ! are disabled, so ignore them */
2814 if (!(name = sysv_translate_name(*p))) {
2819 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2820 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2824 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, "multi-user.target", NULL, true)) < 0)
2828 if ((r = unit_add_dependency(service, UNIT_AFTER, previous, true)) < 0)
2831 if (**p != '@') /* daemons prefixed with @ can be started in the background */
2837 zero(runlevel_services);
2839 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2840 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2845 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2853 if (!(d = opendir(path))) {
2854 if (errno != ENOENT)
2855 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2860 while ((de = readdir(d))) {
2863 if (ignore_file(de->d_name))
2866 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2869 if (strlen(de->d_name) < 4)
2872 a = undecchar(de->d_name[1]);
2873 b = undecchar(de->d_name[2]);
2880 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2885 if (access(fpath, X_OK) < 0) {
2887 if (errno != ENOENT)
2888 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2894 if (!(name = sysv_translate_name(de->d_name + 3))) {
2899 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2900 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2904 if (de->d_name[0] == 'S') {
2906 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
2907 SERVICE(service)->sysv_start_priority =
2908 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2910 SERVICE(service)->sysv_enabled = true;
2913 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
2916 if ((r = set_put(runlevel_services[i], service)) < 0)
2919 } else if (de->d_name[0] == 'K' &&
2920 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2921 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2923 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
2926 if ((r = set_put(shutdown_services, service)) < 0)
2932 /* Now we loaded all stubs and are aware of the lowest
2933 start-up priority for all services, not let's actually load
2934 the services, this will also tell us which services are
2935 actually native now */
2936 manager_dispatch_load_queue(m);
2938 /* If this is a native service, rely on native ways to pull in
2939 * a service, don't pull it in via sysv rcN.d links. */
2940 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
2941 SET_FOREACH(service, runlevel_services[i], j) {
2942 service = unit_follow_merge(service);
2944 if (service->meta.fragment_path)
2947 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2951 /* We honour K links only for halt/reboot. For the normal
2952 * runlevels we assume the stop jobs will be implicitly added
2953 * by the core logic. Also, we don't really distuingish here
2954 * between the runlevels 0 and 6 and just add them to the
2955 * special shutdown target. On SUSE the boot.d/ runlevel is
2956 * also used for shutdown, so we add links for that too to the
2957 * shutdown target.*/
2958 SET_FOREACH(service, shutdown_services, j) {
2959 service = unit_follow_merge(service);
2961 if (service->meta.fragment_path)
2964 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2976 free(arch_daemons_split);
2979 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
2980 set_free(runlevel_services[i]);
2981 set_free(shutdown_services);
2990 static void service_bus_name_owner_change(
2993 const char *old_owner,
2994 const char *new_owner) {
2996 Service *s = SERVICE(u);
3001 assert(streq(s->bus_name, name));
3002 assert(old_owner || new_owner);
3004 if (old_owner && new_owner)
3005 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
3007 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
3009 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
3011 s->bus_name_good = !!new_owner;
3013 if (s->type == SERVICE_DBUS) {
3015 /* service_enter_running() will figure out what to
3017 if (s->state == SERVICE_RUNNING)
3018 service_enter_running(s, true);
3019 else if (s->state == SERVICE_START && new_owner)
3020 service_enter_start_post(s);
3022 } else if (new_owner &&
3024 (s->state == SERVICE_START ||
3025 s->state == SERVICE_START_POST ||
3026 s->state == SERVICE_RUNNING ||
3027 s->state == SERVICE_RELOAD)) {
3029 /* Try to acquire PID from bus service */
3030 log_debug("Trying to acquire PID from D-Bus name...");
3032 bus_query_pid(u->meta.manager, name);
3036 static void service_bus_query_pid_done(
3041 Service *s = SERVICE(u);
3046 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
3048 if (s->main_pid <= 0 &&
3049 (s->state == SERVICE_START ||
3050 s->state == SERVICE_START_POST ||
3051 s->state == SERVICE_RUNNING ||
3052 s->state == SERVICE_RELOAD))
3053 service_set_main_pid(s, pid);
3056 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3060 /* This is called by the socket code when instantiating a new
3061 * service for a stream socket and the socket needs to be
3064 if (s->meta.load_state != UNIT_LOADED)
3067 if (s->socket_fd >= 0)
3070 if (s->state != SERVICE_DEAD)
3074 s->got_socket_fd = true;
3075 s->accept_socket = sock;
3080 static void service_reset_failed(Unit *u) {
3081 Service *s = SERVICE(u);
3085 if (s->state == SERVICE_FAILED)
3086 service_set_state(s, SERVICE_DEAD);
3091 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3092 [SERVICE_DEAD] = "dead",
3093 [SERVICE_START_PRE] = "start-pre",
3094 [SERVICE_START] = "start",
3095 [SERVICE_START_POST] = "start-post",
3096 [SERVICE_RUNNING] = "running",
3097 [SERVICE_EXITED] = "exited",
3098 [SERVICE_RELOAD] = "reload",
3099 [SERVICE_STOP] = "stop",
3100 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3101 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3102 [SERVICE_STOP_POST] = "stop-post",
3103 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3104 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3105 [SERVICE_FAILED] = "failed",
3106 [SERVICE_AUTO_RESTART] = "auto-restart",
3109 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3111 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3112 [SERVICE_RESTART_NO] = "no",
3113 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3114 [SERVICE_RESTART_ALWAYS] = "always",
3117 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3119 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3120 [SERVICE_SIMPLE] = "simple",
3121 [SERVICE_FORKING] = "forking",
3122 [SERVICE_ONESHOT] = "oneshot",
3123 [SERVICE_DBUS] = "dbus",
3124 [SERVICE_NOTIFY] = "notify"
3127 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3129 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3130 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3131 [SERVICE_EXEC_START] = "ExecStart",
3132 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3133 [SERVICE_EXEC_RELOAD] = "ExecReload",
3134 [SERVICE_EXEC_STOP] = "ExecStop",
3135 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3138 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3140 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3141 [NOTIFY_NONE] = "none",
3142 [NOTIFY_MAIN] = "main",
3143 [NOTIFY_ALL] = "all"
3146 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3148 const UnitVTable service_vtable = {
3149 .suffix = ".service",
3150 .show_status = true,
3152 .init = service_init,
3153 .done = service_done,
3154 .load = service_load,
3156 .coldplug = service_coldplug,
3158 .dump = service_dump,
3160 .start = service_start,
3161 .stop = service_stop,
3162 .reload = service_reload,
3164 .can_reload = service_can_reload,
3166 .serialize = service_serialize,
3167 .deserialize_item = service_deserialize_item,
3169 .active_state = service_active_state,
3170 .sub_state_to_string = service_sub_state_to_string,
3172 #ifdef HAVE_SYSV_COMPAT
3173 .check_gc = service_check_gc,
3175 .check_snapshot = service_check_snapshot,
3177 .sigchld_event = service_sigchld_event,
3178 .timer_event = service_timer_event,
3180 .reset_failed = service_reset_failed,
3182 .cgroup_notify_empty = service_cgroup_notify_event,
3183 .notify_message = service_notify_message,
3185 .bus_name_owner_change = service_bus_name_owner_change,
3186 .bus_query_pid_done = service_bus_query_pid_done,
3188 .bus_interface = "org.freedesktop.systemd1.Service",
3189 .bus_message_handler = bus_service_message_handler,
3190 .bus_invalidating_properties = bus_service_invalidating_properties,
3192 #ifdef HAVE_SYSV_COMPAT
3193 .enumerate = service_enumerate