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;
121 s
->guess_main_pid
= true;
123 exec_context_init(&s
->exec_context
);
125 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
127 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
130 static void service_unwatch_control_pid(Service
*s
) {
133 if (s
->control_pid
<= 0)
136 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
140 static void service_unwatch_main_pid(Service
*s
) {
143 if (s
->main_pid
<= 0)
146 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
150 static int service_set_main_pid(Service
*s
, pid_t pid
) {
161 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid())
162 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
163 s
->meta
.id
, (unsigned long) pid
);
166 s
->main_pid_known
= true;
168 exec_status_start(&s
->main_exec_status
, pid
);
173 static void service_close_socket_fd(Service
*s
) {
176 if (s
->socket_fd
< 0)
179 close_nointr_nofail(s
->socket_fd
);
183 static void service_connection_unref(Service
*s
) {
186 if (!s
->accept_socket
)
189 socket_connection_unref(s
->accept_socket
);
190 s
->accept_socket
= NULL
;
193 static void service_done(Unit
*u
) {
194 Service
*s
= SERVICE(u
);
201 #ifdef HAVE_SYSV_COMPAT
205 free(s
->sysv_runlevels
);
206 s
->sysv_runlevels
= NULL
;
209 free(s
->status_text
);
210 s
->status_text
= NULL
;
212 exec_context_done(&s
->exec_context
);
213 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
214 s
->control_command
= NULL
;
215 s
->main_command
= NULL
;
217 /* This will leak a process, but at least no memory or any of
219 service_unwatch_main_pid(s
);
220 service_unwatch_control_pid(s
);
223 unit_unwatch_bus_name(UNIT(u
), s
->bus_name
);
228 service_close_socket_fd(s
);
229 service_connection_unref(s
);
231 set_free(s
->configured_sockets
);
233 unit_unwatch_timer(u
, &s
->timer_watch
);
236 #ifdef HAVE_SYSV_COMPAT
237 static char *sysv_translate_name(const char *name
) {
240 if (!(r
= new(char, strlen(name
) + sizeof(".service"))))
243 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
244 if (endswith(name
, ".sh"))
245 /* Drop Debian-style .sh suffix */
246 strcpy(stpcpy(r
, name
) - 3, ".service");
249 if (startswith(name
, "boot."))
250 /* Drop SuSE-style boot. prefix */
251 strcpy(stpcpy(r
, name
+ 5), ".service");
253 #ifdef TARGET_FRUGALWARE
254 if (startswith(name
, "rc."))
255 /* Drop Frugalware-style rc. prefix */
256 strcpy(stpcpy(r
, name
+ 3), ".service");
259 /* Normal init scripts */
260 strcpy(stpcpy(r
, name
), ".service");
265 static int sysv_translate_facility(const char *name
, const char *filename
, char **_r
) {
267 /* We silently ignore the $ prefix here. According to the LSB
268 * spec it simply indicates whether something is a
269 * standardized name or a distribution-specific one. Since we
270 * just follow what already exists and do not introduce new
271 * uses or names we don't care who introduced a new name. */
273 static const char * const table
[] = {
274 /* LSB defined facilities */
275 "local_fs", SPECIAL_LOCAL_FS_TARGET
,
276 "network", SPECIAL_NETWORK_TARGET
,
277 "named", SPECIAL_NSS_LOOKUP_TARGET
,
278 "portmap", SPECIAL_RPCBIND_TARGET
,
279 "remote_fs", SPECIAL_REMOTE_FS_TARGET
,
280 "syslog", SPECIAL_SYSLOG_TARGET
,
281 "time", SPECIAL_RTC_SET_TARGET
,
283 /* common extensions */
284 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
285 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
,
288 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
289 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
293 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
294 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
295 "httpd", SPECIAL_HTTP_DAEMON_TARGET
,
299 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
310 n
= *name
== '$' ? name
+ 1 : name
;
312 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2) {
314 if (!streq(table
[i
], n
))
320 if (!(r
= strdup(table
[i
+1])))
326 /* If we don't know this name, fallback heuristics to figure
327 * out whether something is a target or a service alias. */
330 if (!unit_prefix_is_valid(n
))
333 /* Facilities starting with $ are most likely targets */
334 r
= unit_name_build(n
, NULL
, ".target");
335 } else if (filename
&& streq(name
, filename
))
336 /* Names equalling the file name of the services are redundant */
339 /* Everything else we assume to be normal service names */
340 r
= sysv_translate_name(n
);
353 static int sysv_fix_order(Service
*s
) {
359 if (s
->sysv_start_priority
< 0)
362 /* For each pair of services where at least one lacks a LSB
363 * header, we use the start priority value to order things. */
365 LIST_FOREACH(units_per_type
, other
, s
->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
368 bool special_s
, special_t
;
370 t
= (Service
*) other
;
375 if (t
->meta
.load_state
!= UNIT_LOADED
)
378 if (t
->sysv_start_priority
< 0)
381 /* If both units have modern headers we don't care
382 * about the priorities */
383 if ((s
->meta
.fragment_path
|| s
->sysv_has_lsb
) &&
384 (t
->meta
.fragment_path
|| t
->sysv_has_lsb
))
387 special_s
= s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
);
388 special_t
= t
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, t
->sysv_runlevels
);
390 if (special_t
&& !special_s
)
392 else if (special_s
&& !special_t
)
394 else if (t
->sysv_start_priority
< s
->sysv_start_priority
)
396 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
401 /* FIXME: Maybe we should compare the name here lexicographically? */
403 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
410 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
413 if (!(c
= new0(ExecCommand
, 1)))
416 if (!(c
->path
= strdup(path
))) {
421 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
430 static int sysv_exec_commands(Service
*s
) {
434 assert(s
->sysv_path
);
436 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
438 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
440 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
442 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
444 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
446 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
451 static int service_load_sysv_path(Service
*s
, const char *path
) {
462 char *short_description
= NULL
, *long_description
= NULL
, *chkconfig_description
= NULL
, *description
;
469 if (!(f
= fopen(path
, "re"))) {
470 r
= errno
== ENOENT
? 0 : -errno
;
475 if (!(s
->sysv_path
= strdup(path
))) {
481 char l
[LINE_MAX
], *t
;
483 if (!fgets(l
, sizeof(l
), f
)) {
488 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
498 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
500 s
->sysv_has_lsb
= true;
504 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
510 t
+= strspn(t
, WHITESPACE
);
512 if (state
== NORMAL
) {
514 /* Try to parse Red Hat style chkconfig headers */
516 if (startswith_no_case(t
, "chkconfig:")) {
518 char runlevels
[16], *k
;
522 if (sscanf(t
+10, "%15s %i %*i",
524 &start_priority
) != 2) {
526 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
530 /* A start priority gathered from the
531 * symlink farms is preferred over the
532 * data from the LSB header. */
533 if (start_priority
< 0 || start_priority
> 99)
534 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
535 else if (s
->sysv_start_priority
< 0)
536 s
->sysv_start_priority
= start_priority
;
538 char_array_0(runlevels
);
539 k
= delete_chars(runlevels
, WHITESPACE
"-");
544 if (!(d
= strdup(k
))) {
549 free(s
->sysv_runlevels
);
550 s
->sysv_runlevels
= d
;
553 } else if (startswith_no_case(t
, "description:")) {
555 size_t k
= strlen(t
);
559 if (t
[k
-1] == '\\') {
564 if ((j
= strstrip(t
+12)) && *j
) {
565 if (!(d
= strdup(j
))) {
572 free(chkconfig_description
);
573 chkconfig_description
= d
;
575 } else if (startswith_no_case(t
, "pidfile:")) {
582 if (!path_is_absolute(fn
)) {
583 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
587 if (!(fn
= strdup(fn
))) {
596 } else if (state
== DESCRIPTION
) {
598 /* Try to parse Red Hat style description
601 size_t k
= strlen(t
);
609 if ((j
= strstrip(t
)) && *j
) {
612 if (chkconfig_description
)
613 asprintf(&d
, "%s %s", chkconfig_description
, j
);
622 free(chkconfig_description
);
623 chkconfig_description
= d
;
626 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
628 if (startswith_no_case(t
, "Provides:")) {
634 FOREACH_WORD_QUOTED(w
, z
, t
+9, i
) {
637 if (!(n
= strndup(w
, z
))) {
642 r
= sysv_translate_facility(n
, file_name_from_path(path
), &m
);
651 if (unit_name_to_type(m
) == UNIT_SERVICE
)
652 r
= unit_add_name(u
, m
);
654 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
, NULL
, true);
656 if (s
->sysv_enabled
) {
659 if ((k
= unit_add_dependency_by_name_inverse(u
, UNIT_WANTS
, m
, NULL
, true)) < 0)
665 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path
, line
, m
, strerror(-r
));
670 } else if (startswith_no_case(t
, "Required-Start:") ||
671 startswith_no_case(t
, "Should-Start:") ||
672 startswith_no_case(t
, "X-Start-Before:") ||
673 startswith_no_case(t
, "X-Start-After:")) {
679 FOREACH_WORD_QUOTED(w
, z
, strchr(t
, ':')+1, i
) {
682 if (!(n
= strndup(w
, z
))) {
687 r
= sysv_translate_facility(n
, file_name_from_path(path
), &m
);
690 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path
, line
, n
, strerror(-r
));
700 r
= unit_add_dependency_by_name(u
, startswith_no_case(t
, "X-Start-Before:") ? UNIT_BEFORE
: UNIT_AFTER
, m
, NULL
, true);
703 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path
, line
, m
, strerror(-r
));
707 } else if (startswith_no_case(t
, "Default-Start:")) {
712 k
= delete_chars(t
+14, WHITESPACE
"-");
715 if (!(d
= strdup(k
))) {
720 free(s
->sysv_runlevels
);
721 s
->sysv_runlevels
= d
;
724 } else if (startswith_no_case(t
, "Description:")) {
727 state
= LSB_DESCRIPTION
;
729 if ((j
= strstrip(t
+12)) && *j
) {
730 if (!(d
= strdup(j
))) {
737 free(long_description
);
738 long_description
= d
;
740 } else if (startswith_no_case(t
, "Short-Description:")) {
745 if ((j
= strstrip(t
+18)) && *j
) {
746 if (!(d
= strdup(j
))) {
753 free(short_description
);
754 short_description
= d
;
756 } else if (startswith_no_case(t
, "X-Interactive:")) {
759 if ((b
= parse_boolean(strstrip(t
+14))) < 0) {
760 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path
, line
);
765 s
->exec_context
.std_input
= EXEC_INPUT_TTY
;
767 s
->exec_context
.std_input
= EXEC_INPUT_NULL
;
769 } else if (state
== LSB_DESCRIPTION
) {
771 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
774 if ((j
= strstrip(t
)) && *j
) {
777 if (long_description
)
778 asprintf(&d
, "%s %s", long_description
, t
);
787 free(long_description
);
788 long_description
= d
;
797 if ((r
= sysv_exec_commands(s
)) < 0)
800 if (s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
)) {
801 /* If there a runlevels configured for this service
802 * but none of the standard ones, then we assume this
803 * is some special kind of service (which might be
804 * needed for early boot) and don't create any links
807 s
->meta
.default_dependencies
= false;
809 /* Don't timeout special services during boot (like fsck) */
812 s
->timeout_usec
= DEFAULT_SYSV_TIMEOUT_USEC
;
814 /* Special setting for all SysV services */
815 s
->type
= SERVICE_FORKING
;
816 s
->remain_after_exit
= true;
817 s
->restart
= SERVICE_RESTART_NO
;
818 s
->exec_context
.std_output
=
819 (s
->meta
.manager
->sysv_console
|| s
->exec_context
.std_input
== EXEC_INPUT_TTY
)
820 ? EXEC_OUTPUT_TTY
: EXEC_OUTPUT_NULL
;
821 s
->exec_context
.kill_mode
= KILL_PROCESS_GROUP
;
823 /* We use the long description only if
824 * no short description is set. */
826 if (short_description
)
827 description
= short_description
;
828 else if (chkconfig_description
)
829 description
= chkconfig_description
;
830 else if (long_description
)
831 description
= long_description
;
838 if (!(d
= strappend("LSB: ", description
))) {
843 u
->meta
.description
= d
;
846 u
->meta
.load_state
= UNIT_LOADED
;
854 free(short_description
);
855 free(long_description
);
856 free(chkconfig_description
);
861 static int service_load_sysv_name(Service
*s
, const char *name
) {
867 /* For SysV services we strip the boot.*, rc.* and *.sh
868 * prefixes/suffixes. */
869 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
870 if (endswith(name
, ".sh.service"))
875 if (startswith(name
, "boot."))
879 #ifdef TARGET_FRUGALWARE
880 if (startswith(name
, "rc."))
884 STRV_FOREACH(p
, s
->meta
.manager
->lookup_paths
.sysvinit_path
) {
888 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
891 assert(endswith(path
, ".service"));
892 path
[strlen(path
)-8] = 0;
894 r
= service_load_sysv_path(s
, path
);
896 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
897 if (r
>= 0 && s
->meta
.load_state
== UNIT_STUB
) {
898 /* Try Debian style *.sh source'able init scripts */
900 r
= service_load_sysv_path(s
, path
);
906 if (r
>= 0 && s
->meta
.load_state
== UNIT_STUB
) {
907 /* Try SUSE style boot.* init scripts */
909 if (asprintf(&path
, "%s/boot.%s", *p
, name
) < 0)
912 /* Drop .service suffix */
913 path
[strlen(path
)-8] = 0;
914 r
= service_load_sysv_path(s
, path
);
919 #ifdef TARGET_FRUGALWARE
920 if (r
>= 0 && s
->meta
.load_state
== UNIT_STUB
) {
921 /* Try Frugalware style rc.* init scripts */
923 if (asprintf(&path
, "%s/rc.%s", *p
, name
) < 0)
926 /* Drop .service suffix */
927 path
[strlen(path
)-8] = 0;
928 r
= service_load_sysv_path(s
, path
);
936 if ((s
->meta
.load_state
!= UNIT_STUB
))
943 static int service_load_sysv(Service
*s
) {
950 /* Load service data from SysV init scripts, preferably with
953 if (strv_isempty(s
->meta
.manager
->lookup_paths
.sysvinit_path
))
956 if ((t
= s
->meta
.id
))
957 if ((r
= service_load_sysv_name(s
, t
)) < 0)
960 if (s
->meta
.load_state
== UNIT_STUB
)
961 SET_FOREACH(t
, s
->meta
.names
, i
) {
965 if ((r
= service_load_sysv_name(s
, t
)) < 0)
968 if (s
->meta
.load_state
!= UNIT_STUB
)
976 static int fsck_fix_order(Service
*s
) {
982 if (s
->fsck_passno
<= 0)
985 /* For each pair of services where both have an fsck priority
986 * we order things based on it. */
988 LIST_FOREACH(units_per_type
, other
, s
->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
992 t
= (Service
*) other
;
997 if (t
->meta
.load_state
!= UNIT_LOADED
)
1000 if (t
->fsck_passno
<= 0)
1003 if (t
->fsck_passno
< s
->fsck_passno
)
1005 else if (t
->fsck_passno
> s
->fsck_passno
)
1010 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
1017 static int service_verify(Service
*s
) {
1020 if (s
->meta
.load_state
!= UNIT_LOADED
)
1023 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
1024 log_error("%s lacks ExecStart setting. Refusing.", s
->meta
.id
);
1028 if (s
->type
!= SERVICE_ONESHOT
&&
1029 s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
1030 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s
->meta
.id
);
1034 if (s
->type
== SERVICE_ONESHOT
&&
1035 s
->exec_command
[SERVICE_EXEC_RELOAD
]) {
1036 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", s
->meta
.id
);
1040 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
1041 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s
->meta
.id
);
1045 if (s
->exec_context
.pam_name
&& s
->exec_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
1046 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s
->meta
.id
);
1053 static int service_add_default_dependencies(Service
*s
) {
1058 /* Add a number of automatic dependencies useful for the
1059 * majority of services. */
1061 /* First, pull in base system */
1062 if (s
->meta
.manager
->running_as
== MANAGER_SYSTEM
) {
1064 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
1067 } else if (s
->meta
.manager
->running_as
== MANAGER_USER
) {
1069 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SOCKETS_TARGET
, NULL
, true)) < 0)
1073 /* Second, activate normal shutdown */
1074 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
1077 static int service_load(Unit
*u
) {
1079 Service
*s
= SERVICE(u
);
1083 /* Load a .service file */
1084 if ((r
= unit_load_fragment(u
)) < 0)
1087 #ifdef HAVE_SYSV_COMPAT
1088 /* Load a classic init script as a fallback, if we couldn't find anything */
1089 if (u
->meta
.load_state
== UNIT_STUB
)
1090 if ((r
= service_load_sysv(s
)) < 0)
1094 /* Still nothing found? Then let's give up */
1095 if (u
->meta
.load_state
== UNIT_STUB
)
1098 /* We were able to load something, then let's add in the
1099 * dropin directories. */
1100 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
1103 /* This is a new unit? Then let's add in some extras */
1104 if (u
->meta
.load_state
== UNIT_LOADED
) {
1105 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
1108 if ((r
= unit_add_default_cgroups(u
)) < 0)
1111 #ifdef HAVE_SYSV_COMPAT
1112 if ((r
= sysv_fix_order(s
)) < 0)
1116 if ((r
= fsck_fix_order(s
)) < 0)
1120 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
1123 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
1124 s
->notify_access
= NOTIFY_MAIN
;
1126 if (s
->type
== SERVICE_DBUS
|| s
->bus_name
)
1127 if ((r
= unit_add_two_dependencies_by_name(u
, UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_DBUS_TARGET
, NULL
, true)) < 0)
1130 if (s
->meta
.default_dependencies
)
1131 if ((r
= service_add_default_dependencies(s
)) < 0)
1135 return service_verify(s
);
1138 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
1140 ServiceExecCommand c
;
1141 Service
*s
= SERVICE(u
);
1142 const char *prefix2
;
1147 p2
= strappend(prefix
, "\t");
1148 prefix2
= p2
? p2
: prefix
;
1151 "%sService State: %s\n"
1152 "%sPermissionsStartOnly: %s\n"
1153 "%sRootDirectoryStartOnly: %s\n"
1154 "%sRemainAfterExit: %s\n"
1155 "%sGuessMainPID: %s\n"
1158 "%sNotifyAccess: %s\n",
1159 prefix
, service_state_to_string(s
->state
),
1160 prefix
, yes_no(s
->permissions_start_only
),
1161 prefix
, yes_no(s
->root_directory_start_only
),
1162 prefix
, yes_no(s
->remain_after_exit
),
1163 prefix
, yes_no(s
->guess_main_pid
),
1164 prefix
, service_type_to_string(s
->type
),
1165 prefix
, service_restart_to_string(s
->restart
),
1166 prefix
, notify_access_to_string(s
->notify_access
));
1168 if (s
->control_pid
> 0)
1170 "%sControl PID: %lu\n",
1171 prefix
, (unsigned long) s
->control_pid
);
1173 if (s
->main_pid
> 0)
1175 "%sMain PID: %lu\n",
1176 prefix
, (unsigned long) s
->main_pid
);
1181 prefix
, s
->pid_file
);
1186 "%sBus Name Good: %s\n",
1187 prefix
, s
->bus_name
,
1188 prefix
, yes_no(s
->bus_name_good
));
1190 exec_context_dump(&s
->exec_context
, f
, prefix
);
1192 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
1194 if (!s
->exec_command
[c
])
1197 fprintf(f
, "%s-> %s:\n",
1198 prefix
, service_exec_command_to_string(c
));
1200 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
1203 #ifdef HAVE_SYSV_COMPAT
1206 "%sSysV Init Script Path: %s\n"
1207 "%sSysV Init Script has LSB Header: %s\n"
1208 "%sSysVEnabled: %s\n",
1209 prefix
, s
->sysv_path
,
1210 prefix
, yes_no(s
->sysv_has_lsb
),
1211 prefix
, yes_no(s
->sysv_enabled
));
1213 if (s
->sysv_start_priority
>= 0)
1215 "%sSysVStartPriority: %i\n",
1216 prefix
, s
->sysv_start_priority
);
1218 if (s
->sysv_runlevels
)
1219 fprintf(f
, "%sSysVRunLevels: %s\n",
1220 prefix
, s
->sysv_runlevels
);
1223 if (s
->fsck_passno
> 0)
1225 "%sFsckPassNo: %i\n",
1226 prefix
, s
->fsck_passno
);
1229 fprintf(f
, "%sStatus Text: %s\n",
1230 prefix
, s
->status_text
);
1235 static int service_load_pid_file(Service
*s
) {
1245 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
1248 r
= parse_pid(k
, &pid
);
1254 if (kill(pid
, 0) < 0 && errno
!= EPERM
) {
1255 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1256 (unsigned long) pid
, s
->pid_file
);
1260 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1263 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1264 /* FIXME: we need to do something here */
1270 static int service_search_main_pid(Service
*s
) {
1276 /* If we know it anyway, don't ever fallback to unreliable
1278 if (s
->main_pid_known
)
1281 if (!s
->guess_main_pid
)
1284 assert(s
->main_pid
<= 0);
1286 if ((pid
= cgroup_bonding_search_main_pid_list(s
->meta
.cgroup_bondings
)) <= 0)
1289 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1292 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1293 /* FIXME: we need to do something here */
1299 static int service_get_sockets(Service
*s
, Set
**_set
) {
1308 if (s
->socket_fd
>= 0)
1311 if (!set_isempty(s
->configured_sockets
))
1314 /* Collects all Socket objects that belong to this
1315 * service. Note that a service might have multiple sockets
1316 * via multiple names. */
1318 if (!(set
= set_new(NULL
, NULL
)))
1321 SET_FOREACH(t
, s
->meta
.names
, i
) {
1325 /* Look for all socket objects that go by any of our
1326 * units and collect their fds */
1328 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
1333 p
= manager_get_unit(s
->meta
.manager
, k
);
1339 if ((r
= set_put(set
, p
)) < 0)
1351 static int service_notify_sockets_dead(Service
*s
) {
1353 Set
*set
, *free_set
= NULL
;
1359 /* Notifies all our sockets when we die */
1361 if (s
->socket_fd
>= 0)
1364 if (!set_isempty(s
->configured_sockets
))
1365 set
= s
->configured_sockets
;
1367 if ((r
= service_get_sockets(s
, &free_set
)) < 0)
1373 SET_FOREACH(sock
, set
, i
)
1374 socket_notify_service_dead(sock
);
1381 static void service_set_state(Service
*s
, ServiceState state
) {
1382 ServiceState old_state
;
1385 old_state
= s
->state
;
1388 if (state
!= SERVICE_START_PRE
&&
1389 state
!= SERVICE_START
&&
1390 state
!= SERVICE_START_POST
&&
1391 state
!= SERVICE_RELOAD
&&
1392 state
!= SERVICE_STOP
&&
1393 state
!= SERVICE_STOP_SIGTERM
&&
1394 state
!= SERVICE_STOP_SIGKILL
&&
1395 state
!= SERVICE_STOP_POST
&&
1396 state
!= SERVICE_FINAL_SIGTERM
&&
1397 state
!= SERVICE_FINAL_SIGKILL
&&
1398 state
!= SERVICE_AUTO_RESTART
)
1399 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1401 if (state
!= SERVICE_START
&&
1402 state
!= SERVICE_START_POST
&&
1403 state
!= SERVICE_RUNNING
&&
1404 state
!= SERVICE_RELOAD
&&
1405 state
!= SERVICE_STOP
&&
1406 state
!= SERVICE_STOP_SIGTERM
&&
1407 state
!= SERVICE_STOP_SIGKILL
) {
1408 service_unwatch_main_pid(s
);
1409 s
->main_command
= NULL
;
1412 if (state
!= SERVICE_START_PRE
&&
1413 state
!= SERVICE_START
&&
1414 state
!= SERVICE_START_POST
&&
1415 state
!= SERVICE_RELOAD
&&
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 service_unwatch_control_pid(s
);
1423 s
->control_command
= NULL
;
1424 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1427 if (state
== SERVICE_DEAD
||
1428 state
== SERVICE_STOP
||
1429 state
== SERVICE_STOP_SIGTERM
||
1430 state
== SERVICE_STOP_SIGKILL
||
1431 state
== SERVICE_STOP_POST
||
1432 state
== SERVICE_FINAL_SIGTERM
||
1433 state
== SERVICE_FINAL_SIGKILL
||
1434 state
== SERVICE_FAILED
||
1435 state
== SERVICE_AUTO_RESTART
)
1436 service_notify_sockets_dead(s
);
1438 if (state
!= SERVICE_START_PRE
&&
1439 state
!= SERVICE_START
&&
1440 state
!= SERVICE_START_POST
&&
1441 state
!= SERVICE_RUNNING
&&
1442 state
!= SERVICE_RELOAD
&&
1443 state
!= SERVICE_STOP
&&
1444 state
!= SERVICE_STOP_SIGTERM
&&
1445 state
!= SERVICE_STOP_SIGKILL
&&
1446 state
!= SERVICE_STOP_POST
&&
1447 state
!= SERVICE_FINAL_SIGTERM
&&
1448 state
!= SERVICE_FINAL_SIGKILL
&&
1449 !(state
== SERVICE_DEAD
&& s
->meta
.job
)) {
1450 service_close_socket_fd(s
);
1451 service_connection_unref(s
);
1454 /* For the inactive states unit_notify() will trim the cgroup,
1455 * but for exit we have to do that ourselves... */
1456 if (state
== SERVICE_EXITED
)
1457 cgroup_bonding_trim_list(s
->meta
.cgroup_bondings
, true);
1459 if (old_state
!= state
)
1460 log_debug("%s changed %s -> %s", s
->meta
.id
, service_state_to_string(old_state
), service_state_to_string(state
));
1462 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], !s
->reload_failure
);
1463 s
->reload_failure
= false;
1466 static int service_coldplug(Unit
*u
) {
1467 Service
*s
= SERVICE(u
);
1471 assert(s
->state
== SERVICE_DEAD
);
1473 if (s
->deserialized_state
!= s
->state
) {
1475 if (s
->deserialized_state
== SERVICE_START_PRE
||
1476 s
->deserialized_state
== SERVICE_START
||
1477 s
->deserialized_state
== SERVICE_START_POST
||
1478 s
->deserialized_state
== SERVICE_RELOAD
||
1479 s
->deserialized_state
== SERVICE_STOP
||
1480 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1481 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1482 s
->deserialized_state
== SERVICE_STOP_POST
||
1483 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1484 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
||
1485 s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
1487 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
|| s
->timeout_usec
> 0) {
1490 k
= s
->deserialized_state
== SERVICE_AUTO_RESTART
? s
->restart_usec
: s
->timeout_usec
;
1492 if ((r
= unit_watch_timer(UNIT(s
), k
, &s
->timer_watch
)) < 0)
1497 if ((s
->deserialized_state
== SERVICE_START
&&
1498 (s
->type
== SERVICE_FORKING
||
1499 s
->type
== SERVICE_DBUS
||
1500 s
->type
== SERVICE_ONESHOT
||
1501 s
->type
== SERVICE_NOTIFY
)) ||
1502 s
->deserialized_state
== SERVICE_START_POST
||
1503 s
->deserialized_state
== SERVICE_RUNNING
||
1504 s
->deserialized_state
== SERVICE_RELOAD
||
1505 s
->deserialized_state
== SERVICE_STOP
||
1506 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1507 s
->deserialized_state
== SERVICE_STOP_SIGKILL
)
1508 if (s
->main_pid
> 0)
1509 if ((r
= unit_watch_pid(UNIT(s
), s
->main_pid
)) < 0)
1512 if (s
->deserialized_state
== SERVICE_START_PRE
||
1513 s
->deserialized_state
== SERVICE_START
||
1514 s
->deserialized_state
== SERVICE_START_POST
||
1515 s
->deserialized_state
== SERVICE_RELOAD
||
1516 s
->deserialized_state
== SERVICE_STOP
||
1517 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1518 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1519 s
->deserialized_state
== SERVICE_STOP_POST
||
1520 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1521 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
)
1522 if (s
->control_pid
> 0)
1523 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1526 service_set_state(s
, s
->deserialized_state
);
1532 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1536 unsigned rn_fds
= 0;
1537 Set
*set
, *free_set
= NULL
;
1544 if (s
->socket_fd
>= 0)
1547 if (!set_isempty(s
->configured_sockets
))
1548 set
= s
->configured_sockets
;
1550 if ((r
= service_get_sockets(s
, &free_set
)) < 0)
1556 SET_FOREACH(sock
, set
, i
) {
1560 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1572 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1578 memcpy(t
, rfds
, rn_fds
);
1579 memcpy(t
+rn_fds
, cfds
, cn_fds
);
1584 rn_fds
= rn_fds
+cn_fds
;
1602 static int service_spawn(
1607 bool apply_permissions
,
1609 bool apply_tty_stdin
,
1610 bool set_notify_socket
,
1615 int *fds
= NULL
, *fdsbuf
= NULL
;
1616 unsigned n_fds
= 0, n_env
= 0;
1617 char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
;
1624 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1625 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1626 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1628 if (s
->socket_fd
>= 0) {
1629 fds
= &s
->socket_fd
;
1632 if ((r
= service_collect_fds(s
, &fdsbuf
, &n_fds
)) < 0)
1639 if (timeout
&& s
->timeout_usec
) {
1640 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1643 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1645 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1650 if (!(our_env
= new0(char*, 4))) {
1655 if (set_notify_socket
)
1656 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=@%s", s
->meta
.manager
->notify_socket
) < 0) {
1661 if (s
->main_pid
> 0)
1662 if (asprintf(our_env
+ n_env
++, "MAINPID=%lu", (unsigned long) s
->main_pid
) < 0) {
1667 if (!(final_env
= strv_env_merge(2,
1668 s
->meta
.manager
->environment
,
1683 s
->meta
.manager
->confirm_spawn
,
1684 s
->meta
.cgroup_bondings
,
1691 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1692 /* FIXME: we need to do something here */
1698 strv_free(final_env
);
1708 strv_free(final_env
);
1711 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1716 static int main_pid_good(Service
*s
) {
1719 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1722 /* If we know the pid file, then lets just check if it is
1724 if (s
->main_pid_known
)
1725 return s
->main_pid
> 0;
1727 /* We don't know the pid */
1731 static int control_pid_good(Service
*s
) {
1734 return s
->control_pid
> 0;
1737 static int cgroup_good(Service
*s
) {
1742 if ((r
= cgroup_bonding_is_empty_list(s
->meta
.cgroup_bondings
)) < 0)
1748 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1755 if (allow_restart
&&
1756 !s
->forbid_restart
&&
1757 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1758 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
) ||
1759 (s
->restart
== SERVICE_RESTART_ON_FAILURE
&& s
->failure
) ||
1760 (s
->restart
== SERVICE_RESTART_ON_ABORT
&& s
->failure
&&
1761 (s
->main_exec_status
.code
== CLD_KILLED
||
1762 s
->main_exec_status
.code
== CLD_DUMPED
)))) {
1764 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1767 service_set_state(s
, SERVICE_AUTO_RESTART
);
1769 service_set_state(s
, s
->failure
? SERVICE_FAILED
: SERVICE_DEAD
);
1771 s
->forbid_restart
= false;
1776 log_warning("%s failed to run install restart timer: %s", s
->meta
.id
, strerror(-r
));
1777 service_enter_dead(s
, false, false);
1780 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1782 static void service_enter_stop_post(Service
*s
, bool success
) {
1789 service_unwatch_control_pid(s
);
1791 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1792 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1794 if ((r
= service_spawn(s
,
1798 !s
->permissions_start_only
,
1799 !s
->root_directory_start_only
,
1802 &s
->control_pid
)) < 0)
1806 service_set_state(s
, SERVICE_STOP_POST
);
1808 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, true);
1813 log_warning("%s failed to run 'stop-post' task: %s", s
->meta
.id
, strerror(-r
));
1814 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1817 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1819 Set
*pid_set
= NULL
;
1820 bool wait_for_exit
= false;
1827 if (s
->exec_context
.kill_mode
!= KILL_NONE
) {
1828 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? s
->exec_context
.kill_signal
: SIGKILL
;
1830 if (s
->main_pid
> 0) {
1831 if (kill(s
->exec_context
.kill_mode
== KILL_PROCESS_GROUP
?
1833 s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1835 log_warning("Failed to kill main process %li: %m", (long) s
->main_pid
);
1837 wait_for_exit
= true;
1840 if (s
->control_pid
> 0) {
1841 if (kill(s
->exec_context
.kill_mode
== KILL_PROCESS_GROUP
?
1843 s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1845 log_warning("Failed to kill control process %li: %m", (long) s
->control_pid
);
1847 wait_for_exit
= true;
1850 if (s
->exec_context
.kill_mode
== KILL_CONTROL_GROUP
) {
1852 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
))) {
1857 /* Exclude the main/control pids from being killed via the cgroup */
1858 if (s
->main_pid
> 0)
1859 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->main_pid
))) < 0)
1862 if (s
->control_pid
> 0)
1863 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0)
1866 if ((r
= cgroup_bonding_kill_list(s
->meta
.cgroup_bondings
, sig
, pid_set
)) < 0) {
1867 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
1868 log_warning("Failed to kill control group: %s", strerror(-r
));
1870 wait_for_exit
= true;
1876 if (wait_for_exit
) {
1877 if (s
->timeout_usec
> 0)
1878 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1881 service_set_state(s
, state
);
1882 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1883 service_enter_stop_post(s
, true);
1885 service_enter_dead(s
, true, true);
1890 log_warning("%s failed to kill processes: %s", s
->meta
.id
, strerror(-r
));
1892 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1893 service_enter_stop_post(s
, false);
1895 service_enter_dead(s
, false, true);
1901 static void service_enter_stop(Service
*s
, bool success
) {
1909 service_unwatch_control_pid(s
);
1911 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
1912 s
->control_command_id
= SERVICE_EXEC_STOP
;
1914 if ((r
= service_spawn(s
,
1918 !s
->permissions_start_only
,
1919 !s
->root_directory_start_only
,
1922 &s
->control_pid
)) < 0)
1925 service_set_state(s
, SERVICE_STOP
);
1927 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1932 log_warning("%s failed to run 'stop' task: %s", s
->meta
.id
, strerror(-r
));
1933 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1936 static void service_enter_running(Service
*s
, bool success
) {
1937 int main_pid_ok
, cgroup_ok
;
1943 main_pid_ok
= main_pid_good(s
);
1944 cgroup_ok
= cgroup_good(s
);
1946 if ((main_pid_ok
> 0 || (main_pid_ok
< 0 && cgroup_ok
!= 0)) &&
1947 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
1948 service_set_state(s
, SERVICE_RUNNING
);
1949 else if (s
->remain_after_exit
)
1950 service_set_state(s
, SERVICE_EXITED
);
1952 service_enter_stop(s
, true);
1955 static void service_enter_start_post(Service
*s
) {
1959 service_unwatch_control_pid(s
);
1961 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
1962 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1964 if ((r
= service_spawn(s
,
1968 !s
->permissions_start_only
,
1969 !s
->root_directory_start_only
,
1972 &s
->control_pid
)) < 0)
1975 service_set_state(s
, SERVICE_START_POST
);
1977 service_enter_running(s
, true);
1982 log_warning("%s failed to run 'start-post' task: %s", s
->meta
.id
, strerror(-r
));
1983 service_enter_stop(s
, false);
1986 static void service_enter_start(Service
*s
) {
1993 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1994 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
|| s
->type
== SERVICE_ONESHOT
);
1996 if (s
->type
== SERVICE_FORKING
)
1997 service_unwatch_control_pid(s
);
1999 service_unwatch_main_pid(s
);
2001 if (s
->type
== SERVICE_FORKING
) {
2002 s
->control_command_id
= SERVICE_EXEC_START
;
2003 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
2005 s
->main_command
= NULL
;
2007 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2008 s
->control_command
= NULL
;
2010 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
2013 if ((r
= service_spawn(s
,
2015 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
,
2020 s
->notify_access
!= NOTIFY_NONE
,
2024 if (s
->type
== SERVICE_SIMPLE
) {
2025 /* For simple services we immediately start
2026 * the START_POST binaries. */
2028 service_set_main_pid(s
, pid
);
2029 service_enter_start_post(s
);
2031 } else if (s
->type
== SERVICE_FORKING
) {
2033 /* For forking services we wait until the start
2034 * process exited. */
2036 s
->control_pid
= pid
;
2037 service_set_state(s
, SERVICE_START
);
2039 } else if (s
->type
== SERVICE_ONESHOT
||
2040 s
->type
== SERVICE_DBUS
||
2041 s
->type
== SERVICE_NOTIFY
) {
2043 /* For oneshot services we wait until the start
2044 * process exited, too, but it is our main process. */
2046 /* For D-Bus services we know the main pid right away,
2047 * but wait for the bus name to appear on the
2048 * bus. Notify services are similar. */
2050 service_set_main_pid(s
, pid
);
2051 service_set_state(s
, SERVICE_START
);
2053 assert_not_reached("Unknown service type");
2058 log_warning("%s failed to run 'start' task: %s", s
->meta
.id
, strerror(-r
));
2059 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2062 static void service_enter_start_pre(Service
*s
) {
2067 service_unwatch_control_pid(s
);
2069 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
2070 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
2072 if ((r
= service_spawn(s
,
2076 !s
->permissions_start_only
,
2077 !s
->root_directory_start_only
,
2080 &s
->control_pid
)) < 0)
2083 service_set_state(s
, SERVICE_START_PRE
);
2085 service_enter_start(s
);
2090 log_warning("%s failed to run 'start-pre' task: %s", s
->meta
.id
, strerror(-r
));
2091 service_enter_dead(s
, false, true);
2094 static void service_enter_restart(Service
*s
) {
2099 dbus_error_init(&error
);
2102 log_info("Job pending for unit, delaying automatic restart.");
2104 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
2108 service_enter_dead(s
, true, false);
2110 if ((r
= manager_add_job(s
->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
)) < 0)
2113 log_debug("%s scheduled restart job.", s
->meta
.id
);
2117 log_warning("%s failed to schedule restart job: %s", s
->meta
.id
, bus_error(&error
, -r
));
2118 service_enter_dead(s
, false, false);
2120 dbus_error_free(&error
);
2123 static void service_enter_reload(Service
*s
) {
2128 service_unwatch_control_pid(s
);
2130 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
2131 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
2133 if ((r
= service_spawn(s
,
2137 !s
->permissions_start_only
,
2138 !s
->root_directory_start_only
,
2141 &s
->control_pid
)) < 0)
2144 service_set_state(s
, SERVICE_RELOAD
);
2146 service_enter_running(s
, true);
2151 log_warning("%s failed to run 'reload' task: %s", s
->meta
.id
, strerror(-r
));
2152 s
->reload_failure
= true;
2153 service_enter_running(s
, true);
2156 static void service_run_next_control(Service
*s
, bool success
) {
2160 assert(s
->control_command
);
2161 assert(s
->control_command
->command_next
);
2166 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
2168 s
->control_command
= s
->control_command
->command_next
;
2169 service_unwatch_control_pid(s
);
2171 if ((r
= service_spawn(s
,
2175 !s
->permissions_start_only
,
2176 !s
->root_directory_start_only
,
2177 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
2178 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
2180 &s
->control_pid
)) < 0)
2186 log_warning("%s failed to run next control task: %s", s
->meta
.id
, strerror(-r
));
2188 if (s
->state
== SERVICE_START_PRE
)
2189 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2190 else if (s
->state
== SERVICE_STOP
)
2191 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
2192 else if (s
->state
== SERVICE_STOP_POST
)
2193 service_enter_dead(s
, false, true);
2194 else if (s
->state
== SERVICE_RELOAD
) {
2195 s
->reload_failure
= true;
2196 service_enter_running(s
, true);
2198 service_enter_stop(s
, false);
2201 static void service_run_next_main(Service
*s
, bool success
) {
2206 assert(s
->main_command
);
2207 assert(s
->main_command
->command_next
);
2208 assert(s
->type
== SERVICE_ONESHOT
);
2213 s
->main_command
= s
->main_command
->command_next
;
2214 service_unwatch_main_pid(s
);
2216 if ((r
= service_spawn(s
,
2223 s
->notify_access
!= NOTIFY_NONE
,
2227 service_set_main_pid(s
, pid
);
2232 log_warning("%s failed to run next main task: %s", s
->meta
.id
, strerror(-r
));
2233 service_enter_stop(s
, false);
2236 static int service_start(Unit
*u
) {
2237 Service
*s
= SERVICE(u
);
2241 /* We cannot fulfill this request right now, try again later
2243 if (s
->state
== SERVICE_STOP
||
2244 s
->state
== SERVICE_STOP_SIGTERM
||
2245 s
->state
== SERVICE_STOP_SIGKILL
||
2246 s
->state
== SERVICE_STOP_POST
||
2247 s
->state
== SERVICE_FINAL_SIGTERM
||
2248 s
->state
== SERVICE_FINAL_SIGKILL
)
2251 /* Already on it! */
2252 if (s
->state
== SERVICE_START_PRE
||
2253 s
->state
== SERVICE_START
||
2254 s
->state
== SERVICE_START_POST
)
2257 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_FAILED
|| s
->state
== SERVICE_AUTO_RESTART
);
2259 /* Make sure we don't enter a busy loop of some kind. */
2260 if (!ratelimit_test(&s
->ratelimit
)) {
2261 log_warning("%s start request repeated too quickly, refusing to start.", u
->meta
.id
);
2266 s
->main_pid_known
= false;
2267 s
->forbid_restart
= false;
2269 service_enter_start_pre(s
);
2273 static int service_stop(Unit
*u
) {
2274 Service
*s
= SERVICE(u
);
2278 /* This is a user request, so don't do restarts on this
2280 s
->forbid_restart
= true;
2283 if (s
->state
== SERVICE_STOP
||
2284 s
->state
== SERVICE_STOP_SIGTERM
||
2285 s
->state
== SERVICE_STOP_SIGKILL
||
2286 s
->state
== SERVICE_STOP_POST
||
2287 s
->state
== SERVICE_FINAL_SIGTERM
||
2288 s
->state
== SERVICE_FINAL_SIGKILL
)
2291 /* Don't allow a restart */
2292 if (s
->state
== SERVICE_AUTO_RESTART
) {
2293 service_set_state(s
, SERVICE_DEAD
);
2297 /* If there's already something running we go directly into
2299 if (s
->state
== SERVICE_START_PRE
||
2300 s
->state
== SERVICE_START
||
2301 s
->state
== SERVICE_START_POST
||
2302 s
->state
== SERVICE_RELOAD
) {
2303 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
2307 assert(s
->state
== SERVICE_RUNNING
||
2308 s
->state
== SERVICE_EXITED
);
2310 service_enter_stop(s
, true);
2314 static int service_reload(Unit
*u
) {
2315 Service
*s
= SERVICE(u
);
2319 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2321 service_enter_reload(s
);
2325 static bool service_can_reload(Unit
*u
) {
2326 Service
*s
= SERVICE(u
);
2330 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2333 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2334 Service
*s
= SERVICE(u
);
2340 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2341 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
2343 if (s
->control_pid
> 0)
2344 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
2346 if (s
->main_pid_known
&& s
->main_pid
> 0)
2347 unit_serialize_item_format(u
, f
, "main-pid", "%lu", (unsigned long) s
->main_pid
);
2349 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2352 unit_serialize_item(u
, f
, "status-text", s
->status_text
);
2354 /* There's a minor uncleanliness here: if there are multiple
2355 * commands attached here, we will start from the first one
2357 if (s
->control_command_id
>= 0)
2358 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2360 if (s
->socket_fd
>= 0) {
2363 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
2366 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
2369 if (s
->main_exec_status
.pid
> 0) {
2370 unit_serialize_item_format(u
, f
, "main-exec-status-pid", "%lu", (unsigned long) s
->main_exec_status
.pid
);
2371 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2372 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2374 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2375 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2376 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2383 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2384 Service
*s
= SERVICE(u
);
2391 if (streq(key
, "state")) {
2394 if ((state
= service_state_from_string(value
)) < 0)
2395 log_debug("Failed to parse state value %s", value
);
2397 s
->deserialized_state
= state
;
2398 } else if (streq(key
, "failure")) {
2401 if ((b
= parse_boolean(value
)) < 0)
2402 log_debug("Failed to parse failure value %s", value
);
2404 s
->failure
= b
|| s
->failure
;
2405 } else if (streq(key
, "control-pid")) {
2408 if (parse_pid(value
, &pid
) < 0)
2409 log_debug("Failed to parse control-pid value %s", value
);
2411 s
->control_pid
= pid
;
2412 } else if (streq(key
, "main-pid")) {
2415 if (parse_pid(value
, &pid
) < 0)
2416 log_debug("Failed to parse main-pid value %s", value
);
2418 service_set_main_pid(s
, (pid_t
) pid
);
2419 } else if (streq(key
, "main-pid-known")) {
2422 if ((b
= parse_boolean(value
)) < 0)
2423 log_debug("Failed to parse main-pid-known value %s", value
);
2425 s
->main_pid_known
= b
;
2426 } else if (streq(key
, "status-text")) {
2429 if ((t
= strdup(value
))) {
2430 free(s
->status_text
);
2434 } else if (streq(key
, "control-command")) {
2435 ServiceExecCommand id
;
2437 if ((id
= service_exec_command_from_string(value
)) < 0)
2438 log_debug("Failed to parse exec-command value %s", value
);
2440 s
->control_command_id
= id
;
2441 s
->control_command
= s
->exec_command
[id
];
2443 } else if (streq(key
, "socket-fd")) {
2446 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2447 log_debug("Failed to parse socket-fd value %s", value
);
2450 if (s
->socket_fd
>= 0)
2451 close_nointr_nofail(s
->socket_fd
);
2452 s
->socket_fd
= fdset_remove(fds
, fd
);
2454 } else if (streq(key
, "main-exec-status-pid")) {
2457 if (parse_pid(value
, &pid
) < 0)
2458 log_debug("Failed to parse main-exec-status-pid value %s", value
);
2460 s
->main_exec_status
.pid
= pid
;
2461 } else if (streq(key
, "main-exec-status-code")) {
2464 if (safe_atoi(value
, &i
) < 0)
2465 log_debug("Failed to parse main-exec-status-code value %s", value
);
2467 s
->main_exec_status
.code
= i
;
2468 } else if (streq(key
, "main-exec-status-status")) {
2471 if (safe_atoi(value
, &i
) < 0)
2472 log_debug("Failed to parse main-exec-status-status value %s", value
);
2474 s
->main_exec_status
.status
= i
;
2475 } else if (streq(key
, "main-exec-status-start"))
2476 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2477 else if (streq(key
, "main-exec-status-exit"))
2478 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2480 log_debug("Unknown serialization key '%s'", key
);
2485 static UnitActiveState
service_active_state(Unit
*u
) {
2488 return state_translation_table
[SERVICE(u
)->state
];
2491 static const char *service_sub_state_to_string(Unit
*u
) {
2494 return service_state_to_string(SERVICE(u
)->state
);
2497 #ifdef HAVE_SYSV_COMPAT
2498 static bool service_check_gc(Unit
*u
) {
2499 Service
*s
= SERVICE(u
);
2503 return !!s
->sysv_path
;
2507 static bool service_check_snapshot(Unit
*u
) {
2508 Service
*s
= SERVICE(u
);
2512 return !s
->got_socket_fd
;
2515 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2516 Service
*s
= SERVICE(u
);
2522 if (!s
->meta
.fragment_path
)
2523 success
= is_clean_exit_lsb(code
, status
);
2525 success
= is_clean_exit(code
, status
);
2527 if (s
->main_pid
== pid
) {
2530 exec_status_exit(&s
->main_exec_status
, pid
, code
, status
, s
->exec_context
.utmp_id
);
2532 /* If this is not a forking service than the main
2533 * process got started and hence we copy the exit
2534 * status so that it is recorded both as main and as
2535 * control process exit status */
2536 if (s
->main_command
) {
2537 s
->main_command
->exec_status
= s
->main_exec_status
;
2539 if (s
->main_command
->ignore
)
2543 log_full(success
? LOG_DEBUG
: LOG_NOTICE
,
2544 "%s: main process exited, code=%s, status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
2545 s
->failure
= s
->failure
|| !success
;
2547 if (s
->main_command
&&
2548 s
->main_command
->command_next
&&
2551 /* There is another command to *
2552 * execute, so let's do that. */
2554 log_debug("%s running next main command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2555 service_run_next_main(s
, success
);
2559 /* The service exited, so the service is officially
2561 s
->main_command
= NULL
;
2565 case SERVICE_START_POST
:
2566 case SERVICE_RELOAD
:
2568 /* Need to wait until the operation is
2573 if (s
->type
== SERVICE_ONESHOT
) {
2574 /* This was our main goal, so let's go on */
2576 service_enter_start_post(s
);
2578 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2581 assert(s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
);
2586 case SERVICE_RUNNING
:
2587 service_enter_running(s
, success
);
2590 case SERVICE_STOP_SIGTERM
:
2591 case SERVICE_STOP_SIGKILL
:
2593 if (!control_pid_good(s
))
2594 service_enter_stop_post(s
, success
);
2596 /* If there is still a control process, wait for that first */
2600 assert_not_reached("Uh, main process died at wrong time.");
2604 } else if (s
->control_pid
== pid
) {
2608 if (s
->control_command
) {
2609 exec_status_exit(&s
->control_command
->exec_status
, pid
, code
, status
, s
->exec_context
.utmp_id
);
2611 if (s
->control_command
->ignore
)
2615 log_full(success
? LOG_DEBUG
: LOG_NOTICE
,
2616 "%s: control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
2617 s
->failure
= s
->failure
|| !success
;
2619 if (s
->control_command
&&
2620 s
->control_command
->command_next
&&
2623 /* There is another command to *
2624 * execute, so let's do that. */
2626 log_debug("%s running next control command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2627 service_run_next_control(s
, success
);
2630 /* No further commands for this step, so let's
2631 * figure out what to do next */
2633 s
->control_command
= NULL
;
2634 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2636 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2640 case SERVICE_START_PRE
:
2642 service_enter_start(s
);
2644 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2648 assert(s
->type
== SERVICE_FORKING
);
2650 /* Let's try to load the pid
2651 * file here if we can. We
2652 * ignore the return value,
2653 * since the PID file might
2654 * actually be created by a
2655 * START_POST script */
2658 service_load_pid_file(s
);
2659 service_search_main_pid(s
);
2661 service_enter_start_post(s
);
2663 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2667 case SERVICE_START_POST
:
2668 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
2671 /* Hmm, let's see if we can
2672 * load the pid now after the
2673 * start-post scripts got
2676 if ((r
= service_load_pid_file(s
)) < 0)
2677 log_warning("%s: failed to load PID file %s: %s", s
->meta
.id
, s
->pid_file
, strerror(-r
));
2680 s
->reload_failure
= !success
;
2681 service_enter_running(s
, true);
2684 case SERVICE_RELOAD
:
2686 service_load_pid_file(s
);
2687 service_search_main_pid(s
);
2690 s
->reload_failure
= !success
;
2691 service_enter_running(s
, true);
2695 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
2698 case SERVICE_STOP_SIGTERM
:
2699 case SERVICE_STOP_SIGKILL
:
2700 if (main_pid_good(s
) <= 0)
2701 service_enter_stop_post(s
, success
);
2703 /* If there is still a service
2704 * process around, wait until
2705 * that one quit, too */
2708 case SERVICE_STOP_POST
:
2709 case SERVICE_FINAL_SIGTERM
:
2710 case SERVICE_FINAL_SIGKILL
:
2711 service_enter_dead(s
, success
, true);
2715 assert_not_reached("Uh, control process died at wrong time.");
2720 /* Notify clients about changed exit status */
2721 unit_add_to_dbus_queue(u
);
2724 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
2725 Service
*s
= SERVICE(u
);
2728 assert(elapsed
== 1);
2730 assert(w
== &s
->timer_watch
);
2734 case SERVICE_START_PRE
:
2736 log_warning("%s operation timed out. Terminating.", u
->meta
.id
);
2737 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2740 case SERVICE_START_POST
:
2741 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
2742 service_enter_stop(s
, false);
2745 case SERVICE_RELOAD
:
2746 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
2747 s
->reload_failure
= true;
2748 service_enter_running(s
, true);
2752 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
2753 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
2756 case SERVICE_STOP_SIGTERM
:
2757 if (s
->exec_context
.send_sigkill
) {
2758 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
2759 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
2761 log_warning("%s stopping timed out. Skipping SIGKILL.", u
->meta
.id
);
2762 service_enter_stop_post(s
, false);
2767 case SERVICE_STOP_SIGKILL
:
2768 /* Uh, wie sent a SIGKILL and it is still not gone?
2769 * Must be something we cannot kill, so let's just be
2770 * weirded out and continue */
2772 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
2773 service_enter_stop_post(s
, false);
2776 case SERVICE_STOP_POST
:
2777 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
2778 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2781 case SERVICE_FINAL_SIGTERM
:
2782 if (s
->exec_context
.send_sigkill
) {
2783 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
2784 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
2786 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u
->meta
.id
);
2787 service_enter_dead(s
, false, true);
2792 case SERVICE_FINAL_SIGKILL
:
2793 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u
->meta
.id
);
2794 service_enter_dead(s
, false, true);
2797 case SERVICE_AUTO_RESTART
:
2798 log_info("%s holdoff time over, scheduling restart.", u
->meta
.id
);
2799 service_enter_restart(s
);
2803 assert_not_reached("Timeout at wrong time.");
2807 static void service_cgroup_notify_event(Unit
*u
) {
2808 Service
*s
= SERVICE(u
);
2812 log_debug("%s: cgroup is empty", u
->meta
.id
);
2816 /* Waiting for SIGCHLD is usually more interesting,
2817 * because it includes return codes/signals. Which is
2818 * why we ignore the cgroup events for most cases,
2819 * except when we don't know pid which to expect the
2822 case SERVICE_RUNNING
:
2823 service_enter_running(s
, true);
2826 case SERVICE_STOP_SIGTERM
:
2827 case SERVICE_STOP_SIGKILL
:
2828 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2829 service_enter_stop_post(s
, true);
2833 case SERVICE_FINAL_SIGTERM
:
2834 case SERVICE_FINAL_SIGKILL
:
2835 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2836 service_enter_dead(s
, true, true);
2845 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
) {
2846 Service
*s
= SERVICE(u
);
2851 if (s
->notify_access
== NOTIFY_NONE
) {
2852 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2853 u
->meta
.id
, (unsigned long) pid
);
2857 if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2858 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2859 u
->meta
.id
, (unsigned long) pid
, (unsigned long) s
->main_pid
);
2863 log_debug("%s: Got message", u
->meta
.id
);
2865 /* Interpret MAINPID= */
2866 if ((e
= strv_find_prefix(tags
, "MAINPID=")) &&
2867 (s
->state
== SERVICE_START
||
2868 s
->state
== SERVICE_START_POST
||
2869 s
->state
== SERVICE_RUNNING
||
2870 s
->state
== SERVICE_RELOAD
)) {
2872 if (parse_pid(e
+ 8, &pid
) < 0)
2873 log_warning("Failed to parse notification message %s", e
);
2875 log_debug("%s: got %s", u
->meta
.id
, e
);
2876 service_set_main_pid(s
, pid
);
2880 /* Interpret READY= */
2881 if (s
->type
== SERVICE_NOTIFY
&&
2882 s
->state
== SERVICE_START
&&
2883 strv_find(tags
, "READY=1")) {
2884 log_debug("%s: got READY=1", u
->meta
.id
);
2886 service_enter_start_post(s
);
2889 /* Interpret STATUS= */
2890 if ((e
= strv_find_prefix(tags
, "STATUS="))) {
2894 if (!(t
= strdup(e
+7))) {
2895 log_error("Failed to allocate string.");
2899 log_debug("%s: got %s", u
->meta
.id
, e
);
2901 free(s
->status_text
);
2904 free(s
->status_text
);
2905 s
->status_text
= NULL
;
2910 /* Notify clients about changed status or main pid */
2911 unit_add_to_dbus_queue(u
);
2914 #ifdef HAVE_SYSV_COMPAT
2915 static int service_enumerate(Manager
*m
) {
2919 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
2920 Set
*runlevel_services
[ELEMENTSOF(rcnd_table
)], *shutdown_services
= NULL
;
2927 zero(runlevel_services
);
2929 STRV_FOREACH(p
, m
->lookup_paths
.sysvrcnd_path
)
2930 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++) {
2935 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
].path
) < 0) {
2943 if (!(d
= opendir(path
))) {
2944 if (errno
!= ENOENT
)
2945 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
2950 while ((de
= readdir(d
))) {
2953 if (ignore_file(de
->d_name
))
2956 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
2959 if (strlen(de
->d_name
) < 4)
2962 a
= undecchar(de
->d_name
[1]);
2963 b
= undecchar(de
->d_name
[2]);
2970 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
].path
, de
->d_name
) < 0) {
2975 if (access(fpath
, X_OK
) < 0) {
2977 if (errno
!= ENOENT
)
2978 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
2984 if (!(name
= sysv_translate_name(de
->d_name
+ 3))) {
2989 if ((r
= manager_load_unit_prepare(m
, name
, NULL
, NULL
, &service
)) < 0) {
2990 log_warning("Failed to prepare unit %s: %s", name
, strerror(-r
));
2994 if (de
->d_name
[0] == 'S') {
2996 if (rcnd_table
[i
].type
== RUNLEVEL_UP
|| rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
) {
2997 SERVICE(service
)->sysv_start_priority
=
2998 MAX(a
*10 + b
, SERVICE(service
)->sysv_start_priority
);
3000 SERVICE(service
)->sysv_enabled
= true;
3003 if ((r
= set_ensure_allocated(&runlevel_services
[i
], trivial_hash_func
, trivial_compare_func
)) < 0)
3006 if ((r
= set_put(runlevel_services
[i
], service
)) < 0)
3009 } else if (de
->d_name
[0] == 'K' &&
3010 (rcnd_table
[i
].type
== RUNLEVEL_DOWN
||
3011 rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
)) {
3013 if ((r
= set_ensure_allocated(&shutdown_services
, trivial_hash_func
, trivial_compare_func
)) < 0)
3016 if ((r
= set_put(shutdown_services
, service
)) < 0)
3022 /* Now we loaded all stubs and are aware of the lowest
3023 start-up priority for all services, not let's actually load
3024 the services, this will also tell us which services are
3025 actually native now */
3026 manager_dispatch_load_queue(m
);
3028 /* If this is a native service, rely on native ways to pull in
3029 * a service, don't pull it in via sysv rcN.d links. */
3030 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
3031 SET_FOREACH(service
, runlevel_services
[i
], j
) {
3032 service
= unit_follow_merge(service
);
3034 if (service
->meta
.fragment_path
)
3037 if ((r
= unit_add_two_dependencies_by_name_inverse(service
, UNIT_AFTER
, UNIT_WANTS
, rcnd_table
[i
].target
, NULL
, true)) < 0)
3041 /* We honour K links only for halt/reboot. For the normal
3042 * runlevels we assume the stop jobs will be implicitly added
3043 * by the core logic. Also, we don't really distuingish here
3044 * between the runlevels 0 and 6 and just add them to the
3045 * special shutdown target. On SUSE the boot.d/ runlevel is
3046 * also used for shutdown, so we add links for that too to the
3047 * shutdown target.*/
3048 SET_FOREACH(service
, shutdown_services
, j
) {
3049 service
= unit_follow_merge(service
);
3051 if (service
->meta
.fragment_path
)
3054 if ((r
= unit_add_two_dependencies_by_name(service
, UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true)) < 0)
3065 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
3066 set_free(runlevel_services
[i
]);
3067 set_free(shutdown_services
);
3076 static void service_bus_name_owner_change(
3079 const char *old_owner
,
3080 const char *new_owner
) {
3082 Service
*s
= SERVICE(u
);
3087 assert(streq(s
->bus_name
, name
));
3088 assert(old_owner
|| new_owner
);
3090 if (old_owner
&& new_owner
)
3091 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->meta
.id
, name
, old_owner
, new_owner
);
3093 log_debug("%s's D-Bus name %s no longer registered by %s", u
->meta
.id
, name
, old_owner
);
3095 log_debug("%s's D-Bus name %s now registered by %s", u
->meta
.id
, name
, new_owner
);
3097 s
->bus_name_good
= !!new_owner
;
3099 if (s
->type
== SERVICE_DBUS
) {
3101 /* service_enter_running() will figure out what to
3103 if (s
->state
== SERVICE_RUNNING
)
3104 service_enter_running(s
, true);
3105 else if (s
->state
== SERVICE_START
&& new_owner
)
3106 service_enter_start_post(s
);
3108 } else if (new_owner
&&
3110 (s
->state
== SERVICE_START
||
3111 s
->state
== SERVICE_START_POST
||
3112 s
->state
== SERVICE_RUNNING
||
3113 s
->state
== SERVICE_RELOAD
)) {
3115 /* Try to acquire PID from bus service */
3116 log_debug("Trying to acquire PID from D-Bus name...");
3118 bus_query_pid(u
->meta
.manager
, name
);
3122 static void service_bus_query_pid_done(
3127 Service
*s
= SERVICE(u
);
3132 log_debug("%s's D-Bus name %s is now owned by process %u", u
->meta
.id
, name
, (unsigned) pid
);
3134 if (s
->main_pid
<= 0 &&
3135 (s
->state
== SERVICE_START
||
3136 s
->state
== SERVICE_START_POST
||
3137 s
->state
== SERVICE_RUNNING
||
3138 s
->state
== SERVICE_RELOAD
))
3139 service_set_main_pid(s
, pid
);
3142 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
) {
3146 /* This is called by the socket code when instantiating a new
3147 * service for a stream socket and the socket needs to be
3150 if (s
->meta
.load_state
!= UNIT_LOADED
)
3153 if (s
->socket_fd
>= 0)
3156 if (s
->state
!= SERVICE_DEAD
)
3160 s
->got_socket_fd
= true;
3161 s
->accept_socket
= sock
;
3166 static void service_reset_failed(Unit
*u
) {
3167 Service
*s
= SERVICE(u
);
3171 if (s
->state
== SERVICE_FAILED
)
3172 service_set_state(s
, SERVICE_DEAD
);
3177 static int service_kill(Unit
*u
, KillWho who
, KillMode mode
, int signo
, DBusError
*error
) {
3178 Service
*s
= SERVICE(u
);
3180 Set
*pid_set
= NULL
;
3184 if (s
->main_pid
<= 0 && who
== KILL_MAIN
) {
3185 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No main process to kill");
3189 if (s
->control_pid
<= 0 && who
== KILL_CONTROL
) {
3190 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No control process to kill");
3194 if (s
->control_pid
> 0)
3195 if (kill(mode
== KILL_PROCESS_GROUP
? -s
->control_pid
: s
->control_pid
, signo
) < 0)
3198 if (s
->main_pid
> 0)
3199 if (kill(mode
== KILL_PROCESS_GROUP
? -s
->main_pid
: s
->main_pid
, signo
) < 0)
3202 if (mode
== KILL_CONTROL_GROUP
) {
3205 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
)))
3208 /* Exclude the control/main pid from being killed via the cgroup */
3209 if (s
->control_pid
> 0)
3210 if ((q
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0) {
3215 if (s
->main_pid
> 0)
3216 if ((q
= set_put(pid_set
, LONG_TO_PTR(s
->main_pid
))) < 0) {
3221 if ((q
= cgroup_bonding_kill_list(s
->meta
.cgroup_bondings
, signo
, pid_set
)) < 0)
3222 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
3233 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
3234 [SERVICE_DEAD
] = "dead",
3235 [SERVICE_START_PRE
] = "start-pre",
3236 [SERVICE_START
] = "start",
3237 [SERVICE_START_POST
] = "start-post",
3238 [SERVICE_RUNNING
] = "running",
3239 [SERVICE_EXITED
] = "exited",
3240 [SERVICE_RELOAD
] = "reload",
3241 [SERVICE_STOP
] = "stop",
3242 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
3243 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
3244 [SERVICE_STOP_POST
] = "stop-post",
3245 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
3246 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
3247 [SERVICE_FAILED
] = "failed",
3248 [SERVICE_AUTO_RESTART
] = "auto-restart",
3251 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
3253 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3254 [SERVICE_RESTART_NO
] = "no",
3255 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3256 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3257 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3258 [SERVICE_RESTART_ALWAYS
] = "always"
3261 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3263 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3264 [SERVICE_SIMPLE
] = "simple",
3265 [SERVICE_FORKING
] = "forking",
3266 [SERVICE_ONESHOT
] = "oneshot",
3267 [SERVICE_DBUS
] = "dbus",
3268 [SERVICE_NOTIFY
] = "notify"
3271 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3273 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3274 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3275 [SERVICE_EXEC_START
] = "ExecStart",
3276 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3277 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3278 [SERVICE_EXEC_STOP
] = "ExecStop",
3279 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3282 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3284 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3285 [NOTIFY_NONE
] = "none",
3286 [NOTIFY_MAIN
] = "main",
3287 [NOTIFY_ALL
] = "all"
3290 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3292 const UnitVTable service_vtable
= {
3293 .suffix
= ".service",
3294 .show_status
= true,
3296 .init
= service_init
,
3297 .done
= service_done
,
3298 .load
= service_load
,
3300 .coldplug
= service_coldplug
,
3302 .dump
= service_dump
,
3304 .start
= service_start
,
3305 .stop
= service_stop
,
3306 .reload
= service_reload
,
3308 .can_reload
= service_can_reload
,
3310 .kill
= service_kill
,
3312 .serialize
= service_serialize
,
3313 .deserialize_item
= service_deserialize_item
,
3315 .active_state
= service_active_state
,
3316 .sub_state_to_string
= service_sub_state_to_string
,
3318 #ifdef HAVE_SYSV_COMPAT
3319 .check_gc
= service_check_gc
,
3321 .check_snapshot
= service_check_snapshot
,
3323 .sigchld_event
= service_sigchld_event
,
3324 .timer_event
= service_timer_event
,
3326 .reset_failed
= service_reset_failed
,
3328 .cgroup_notify_empty
= service_cgroup_notify_event
,
3329 .notify_message
= service_notify_message
,
3331 .bus_name_owner_change
= service_bus_name_owner_change
,
3332 .bus_query_pid_done
= service_bus_query_pid_done
,
3334 .bus_interface
= "org.freedesktop.systemd1.Service",
3335 .bus_message_handler
= bus_service_message_handler
,
3336 .bus_invalidating_properties
= bus_service_invalidating_properties
,
3338 #ifdef HAVE_SYSV_COMPAT
3339 .enumerate
= service_enumerate