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
);
124 s
->exec_context
.std_output
= u
->meta
.manager
->default_std_output
;
125 s
->exec_context
.std_error
= u
->meta
.manager
->default_std_error
;
127 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
129 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
132 static void service_unwatch_control_pid(Service
*s
) {
135 if (s
->control_pid
<= 0)
138 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
142 static void service_unwatch_main_pid(Service
*s
) {
145 if (s
->main_pid
<= 0)
148 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
152 static int service_set_main_pid(Service
*s
, pid_t pid
) {
163 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid())
164 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
165 s
->meta
.id
, (unsigned long) pid
);
168 s
->main_pid_known
= true;
170 exec_status_start(&s
->main_exec_status
, pid
);
175 static void service_close_socket_fd(Service
*s
) {
178 if (s
->socket_fd
< 0)
181 close_nointr_nofail(s
->socket_fd
);
185 static void service_connection_unref(Service
*s
) {
188 if (!s
->accept_socket
)
191 socket_connection_unref(s
->accept_socket
);
192 s
->accept_socket
= NULL
;
195 static void service_done(Unit
*u
) {
196 Service
*s
= SERVICE(u
);
203 #ifdef HAVE_SYSV_COMPAT
207 free(s
->sysv_runlevels
);
208 s
->sysv_runlevels
= NULL
;
211 free(s
->status_text
);
212 s
->status_text
= NULL
;
214 exec_context_done(&s
->exec_context
);
215 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
216 s
->control_command
= NULL
;
217 s
->main_command
= NULL
;
219 /* This will leak a process, but at least no memory or any of
221 service_unwatch_main_pid(s
);
222 service_unwatch_control_pid(s
);
225 unit_unwatch_bus_name(UNIT(u
), s
->bus_name
);
230 service_close_socket_fd(s
);
231 service_connection_unref(s
);
233 set_free(s
->configured_sockets
);
235 unit_unwatch_timer(u
, &s
->timer_watch
);
238 #ifdef HAVE_SYSV_COMPAT
239 static char *sysv_translate_name(const char *name
) {
242 if (!(r
= new(char, strlen(name
) + sizeof(".service"))))
245 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
246 if (endswith(name
, ".sh"))
247 /* Drop Debian-style .sh suffix */
248 strcpy(stpcpy(r
, name
) - 3, ".service");
251 if (startswith(name
, "boot."))
252 /* Drop SuSE-style boot. prefix */
253 strcpy(stpcpy(r
, name
+ 5), ".service");
255 #ifdef TARGET_FRUGALWARE
256 if (startswith(name
, "rc."))
257 /* Drop Frugalware-style rc. prefix */
258 strcpy(stpcpy(r
, name
+ 3), ".service");
261 /* Normal init scripts */
262 strcpy(stpcpy(r
, name
), ".service");
267 static int sysv_translate_facility(const char *name
, const char *filename
, char **_r
) {
269 /* We silently ignore the $ prefix here. According to the LSB
270 * spec it simply indicates whether something is a
271 * standardized name or a distribution-specific one. Since we
272 * just follow what already exists and do not introduce new
273 * uses or names we don't care who introduced a new name. */
275 static const char * const table
[] = {
276 /* LSB defined facilities */
277 "local_fs", SPECIAL_LOCAL_FS_TARGET
,
278 "network", SPECIAL_NETWORK_TARGET
,
279 "named", SPECIAL_NSS_LOOKUP_TARGET
,
280 "portmap", SPECIAL_RPCBIND_TARGET
,
281 "remote_fs", SPECIAL_REMOTE_FS_TARGET
,
282 "syslog", SPECIAL_SYSLOG_TARGET
,
283 "time", SPECIAL_RTC_SET_TARGET
,
285 /* common extensions */
286 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
287 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
,
290 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
291 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
295 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
296 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
297 "httpd", SPECIAL_HTTP_DAEMON_TARGET
,
301 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
312 n
= *name
== '$' ? name
+ 1 : name
;
314 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2) {
316 if (!streq(table
[i
], n
))
322 if (!(r
= strdup(table
[i
+1])))
328 /* If we don't know this name, fallback heuristics to figure
329 * out whether something is a target or a service alias. */
332 if (!unit_prefix_is_valid(n
))
335 /* Facilities starting with $ are most likely targets */
336 r
= unit_name_build(n
, NULL
, ".target");
337 } else if (filename
&& streq(name
, filename
))
338 /* Names equalling the file name of the services are redundant */
341 /* Everything else we assume to be normal service names */
342 r
= sysv_translate_name(n
);
355 static int sysv_fix_order(Service
*s
) {
361 if (s
->sysv_start_priority
< 0)
364 /* For each pair of services where at least one lacks a LSB
365 * header, we use the start priority value to order things. */
367 LIST_FOREACH(units_per_type
, other
, s
->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
370 bool special_s
, special_t
;
372 t
= (Service
*) other
;
377 if (t
->meta
.load_state
!= UNIT_LOADED
)
380 if (t
->sysv_start_priority
< 0)
383 /* If both units have modern headers we don't care
384 * about the priorities */
385 if ((s
->meta
.fragment_path
|| s
->sysv_has_lsb
) &&
386 (t
->meta
.fragment_path
|| t
->sysv_has_lsb
))
389 special_s
= s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
);
390 special_t
= t
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, t
->sysv_runlevels
);
392 if (special_t
&& !special_s
)
394 else if (special_s
&& !special_t
)
396 else if (t
->sysv_start_priority
< s
->sysv_start_priority
)
398 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
403 /* FIXME: Maybe we should compare the name here lexicographically? */
405 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
412 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
415 if (!(c
= new0(ExecCommand
, 1)))
418 if (!(c
->path
= strdup(path
))) {
423 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
432 static int sysv_exec_commands(Service
*s
) {
436 assert(s
->sysv_path
);
438 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
440 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
442 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
444 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
446 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
448 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
453 static int service_load_sysv_path(Service
*s
, const char *path
) {
464 char *short_description
= NULL
, *long_description
= NULL
, *chkconfig_description
= NULL
, *description
;
471 if (!(f
= fopen(path
, "re"))) {
472 r
= errno
== ENOENT
? 0 : -errno
;
477 if (!(s
->sysv_path
= strdup(path
))) {
483 char l
[LINE_MAX
], *t
;
485 if (!fgets(l
, sizeof(l
), f
)) {
490 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
500 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
502 s
->sysv_has_lsb
= true;
506 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
512 t
+= strspn(t
, WHITESPACE
);
514 if (state
== NORMAL
) {
516 /* Try to parse Red Hat style chkconfig headers */
518 if (startswith_no_case(t
, "chkconfig:")) {
520 char runlevels
[16], *k
;
524 if (sscanf(t
+10, "%15s %i %*i",
526 &start_priority
) != 2) {
528 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
532 /* A start priority gathered from the
533 * symlink farms is preferred over the
534 * data from the LSB header. */
535 if (start_priority
< 0 || start_priority
> 99)
536 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
537 else if (s
->sysv_start_priority
< 0)
538 s
->sysv_start_priority
= start_priority
;
540 char_array_0(runlevels
);
541 k
= delete_chars(runlevels
, WHITESPACE
"-");
546 if (!(d
= strdup(k
))) {
551 free(s
->sysv_runlevels
);
552 s
->sysv_runlevels
= d
;
555 } else if (startswith_no_case(t
, "description:")) {
557 size_t k
= strlen(t
);
561 if (t
[k
-1] == '\\') {
566 if ((j
= strstrip(t
+12)) && *j
) {
567 if (!(d
= strdup(j
))) {
574 free(chkconfig_description
);
575 chkconfig_description
= d
;
577 } else if (startswith_no_case(t
, "pidfile:")) {
584 if (!path_is_absolute(fn
)) {
585 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
589 if (!(fn
= strdup(fn
))) {
598 } else if (state
== DESCRIPTION
) {
600 /* Try to parse Red Hat style description
603 size_t k
= strlen(t
);
611 if ((j
= strstrip(t
)) && *j
) {
614 if (chkconfig_description
)
615 asprintf(&d
, "%s %s", chkconfig_description
, j
);
624 free(chkconfig_description
);
625 chkconfig_description
= d
;
628 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
630 if (startswith_no_case(t
, "Provides:")) {
636 FOREACH_WORD_QUOTED(w
, z
, t
+9, i
) {
639 if (!(n
= strndup(w
, z
))) {
644 r
= sysv_translate_facility(n
, file_name_from_path(path
), &m
);
653 if (unit_name_to_type(m
) == UNIT_SERVICE
)
654 r
= unit_add_name(u
, m
);
656 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
, NULL
, true);
658 if (s
->sysv_enabled
) {
661 if ((k
= unit_add_dependency_by_name_inverse(u
, UNIT_WANTS
, m
, NULL
, true)) < 0)
667 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path
, line
, m
, strerror(-r
));
672 } else if (startswith_no_case(t
, "Required-Start:") ||
673 startswith_no_case(t
, "Should-Start:") ||
674 startswith_no_case(t
, "X-Start-Before:") ||
675 startswith_no_case(t
, "X-Start-After:")) {
681 FOREACH_WORD_QUOTED(w
, z
, strchr(t
, ':')+1, i
) {
684 if (!(n
= strndup(w
, z
))) {
689 r
= sysv_translate_facility(n
, file_name_from_path(path
), &m
);
692 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path
, line
, n
, strerror(-r
));
702 r
= unit_add_dependency_by_name(u
, startswith_no_case(t
, "X-Start-Before:") ? UNIT_BEFORE
: UNIT_AFTER
, m
, NULL
, true);
705 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path
, line
, m
, strerror(-r
));
709 } else if (startswith_no_case(t
, "Default-Start:")) {
714 k
= delete_chars(t
+14, WHITESPACE
"-");
717 if (!(d
= strdup(k
))) {
722 free(s
->sysv_runlevels
);
723 s
->sysv_runlevels
= d
;
726 } else if (startswith_no_case(t
, "Description:")) {
729 state
= LSB_DESCRIPTION
;
731 if ((j
= strstrip(t
+12)) && *j
) {
732 if (!(d
= strdup(j
))) {
739 free(long_description
);
740 long_description
= d
;
742 } else if (startswith_no_case(t
, "Short-Description:")) {
747 if ((j
= strstrip(t
+18)) && *j
) {
748 if (!(d
= strdup(j
))) {
755 free(short_description
);
756 short_description
= d
;
758 } else if (startswith_no_case(t
, "X-Interactive:")) {
761 if ((b
= parse_boolean(strstrip(t
+14))) < 0) {
762 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path
, line
);
767 s
->exec_context
.std_input
= EXEC_INPUT_TTY
;
769 s
->exec_context
.std_input
= EXEC_INPUT_NULL
;
771 } else if (state
== LSB_DESCRIPTION
) {
773 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
776 if ((j
= strstrip(t
)) && *j
) {
779 if (long_description
)
780 asprintf(&d
, "%s %s", long_description
, t
);
789 free(long_description
);
790 long_description
= d
;
799 if ((r
= sysv_exec_commands(s
)) < 0)
802 if (s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
)) {
803 /* If there a runlevels configured for this service
804 * but none of the standard ones, then we assume this
805 * is some special kind of service (which might be
806 * needed for early boot) and don't create any links
809 s
->meta
.default_dependencies
= false;
811 /* Don't timeout special services during boot (like fsck) */
814 s
->timeout_usec
= DEFAULT_SYSV_TIMEOUT_USEC
;
816 /* Special setting for all SysV services */
817 s
->type
= SERVICE_FORKING
;
818 s
->remain_after_exit
= true;
819 s
->restart
= SERVICE_RESTART_NO
;
820 s
->exec_context
.std_output
=
821 (s
->meta
.manager
->sysv_console
|| s
->exec_context
.std_input
== EXEC_INPUT_TTY
)
822 ? EXEC_OUTPUT_TTY
: s
->meta
.manager
->default_std_output
;
823 s
->exec_context
.kill_mode
= KILL_PROCESS_GROUP
;
825 /* We use the long description only if
826 * no short description is set. */
828 if (short_description
)
829 description
= short_description
;
830 else if (chkconfig_description
)
831 description
= chkconfig_description
;
832 else if (long_description
)
833 description
= long_description
;
840 if (!(d
= strappend("LSB: ", description
))) {
845 u
->meta
.description
= d
;
848 u
->meta
.load_state
= UNIT_LOADED
;
856 free(short_description
);
857 free(long_description
);
858 free(chkconfig_description
);
863 static int service_load_sysv_name(Service
*s
, const char *name
) {
869 /* For SysV services we strip the boot.*, rc.* and *.sh
870 * prefixes/suffixes. */
871 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
872 if (endswith(name
, ".sh.service"))
877 if (startswith(name
, "boot."))
881 #ifdef TARGET_FRUGALWARE
882 if (startswith(name
, "rc."))
886 STRV_FOREACH(p
, s
->meta
.manager
->lookup_paths
.sysvinit_path
) {
890 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
893 assert(endswith(path
, ".service"));
894 path
[strlen(path
)-8] = 0;
896 r
= service_load_sysv_path(s
, path
);
898 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU)
899 if (r
>= 0 && s
->meta
.load_state
== UNIT_STUB
) {
900 /* Try Debian style *.sh source'able init scripts */
902 r
= service_load_sysv_path(s
, path
);
908 if (r
>= 0 && s
->meta
.load_state
== UNIT_STUB
) {
909 /* Try SUSE style boot.* init scripts */
911 if (asprintf(&path
, "%s/boot.%s", *p
, name
) < 0)
914 /* Drop .service suffix */
915 path
[strlen(path
)-8] = 0;
916 r
= service_load_sysv_path(s
, path
);
921 #ifdef TARGET_FRUGALWARE
922 if (r
>= 0 && s
->meta
.load_state
== UNIT_STUB
) {
923 /* Try Frugalware style rc.* init scripts */
925 if (asprintf(&path
, "%s/rc.%s", *p
, name
) < 0)
928 /* Drop .service suffix */
929 path
[strlen(path
)-8] = 0;
930 r
= service_load_sysv_path(s
, path
);
938 if ((s
->meta
.load_state
!= UNIT_STUB
))
945 static int service_load_sysv(Service
*s
) {
952 /* Load service data from SysV init scripts, preferably with
955 if (strv_isempty(s
->meta
.manager
->lookup_paths
.sysvinit_path
))
958 if ((t
= s
->meta
.id
))
959 if ((r
= service_load_sysv_name(s
, t
)) < 0)
962 if (s
->meta
.load_state
== UNIT_STUB
)
963 SET_FOREACH(t
, s
->meta
.names
, i
) {
967 if ((r
= service_load_sysv_name(s
, t
)) < 0)
970 if (s
->meta
.load_state
!= UNIT_STUB
)
978 static int fsck_fix_order(Service
*s
) {
984 if (s
->fsck_passno
<= 0)
987 /* For each pair of services where both have an fsck priority
988 * we order things based on it. */
990 LIST_FOREACH(units_per_type
, other
, s
->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
994 t
= (Service
*) other
;
999 if (t
->meta
.load_state
!= UNIT_LOADED
)
1002 if (t
->fsck_passno
<= 0)
1005 if (t
->fsck_passno
< s
->fsck_passno
)
1007 else if (t
->fsck_passno
> s
->fsck_passno
)
1012 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
1019 static int service_verify(Service
*s
) {
1022 if (s
->meta
.load_state
!= UNIT_LOADED
)
1025 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
1026 log_error("%s lacks ExecStart setting. Refusing.", s
->meta
.id
);
1030 if (s
->type
!= SERVICE_ONESHOT
&&
1031 s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
1032 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s
->meta
.id
);
1036 if (s
->type
== SERVICE_ONESHOT
&&
1037 s
->exec_command
[SERVICE_EXEC_RELOAD
]) {
1038 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", s
->meta
.id
);
1042 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
1043 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s
->meta
.id
);
1047 if (s
->exec_context
.pam_name
&& s
->exec_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
1048 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s
->meta
.id
);
1055 static int service_add_default_dependencies(Service
*s
) {
1060 /* Add a number of automatic dependencies useful for the
1061 * majority of services. */
1063 /* First, pull in base system */
1064 if (s
->meta
.manager
->running_as
== MANAGER_SYSTEM
) {
1066 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
1069 } else if (s
->meta
.manager
->running_as
== MANAGER_USER
) {
1071 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SOCKETS_TARGET
, NULL
, true)) < 0)
1075 /* Second, activate normal shutdown */
1076 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
1079 static int service_load(Unit
*u
) {
1081 Service
*s
= SERVICE(u
);
1085 /* Load a .service file */
1086 if ((r
= unit_load_fragment(u
)) < 0)
1089 #ifdef HAVE_SYSV_COMPAT
1090 /* Load a classic init script as a fallback, if we couldn't find anything */
1091 if (u
->meta
.load_state
== UNIT_STUB
)
1092 if ((r
= service_load_sysv(s
)) < 0)
1096 /* Still nothing found? Then let's give up */
1097 if (u
->meta
.load_state
== UNIT_STUB
)
1100 /* We were able to load something, then let's add in the
1101 * dropin directories. */
1102 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
1105 /* This is a new unit? Then let's add in some extras */
1106 if (u
->meta
.load_state
== UNIT_LOADED
) {
1107 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
1110 if ((r
= unit_add_default_cgroups(u
)) < 0)
1113 #ifdef HAVE_SYSV_COMPAT
1114 if ((r
= sysv_fix_order(s
)) < 0)
1118 if ((r
= fsck_fix_order(s
)) < 0)
1122 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
1125 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
1126 s
->notify_access
= NOTIFY_MAIN
;
1128 if (s
->type
== SERVICE_DBUS
|| s
->bus_name
)
1129 if ((r
= unit_add_two_dependencies_by_name(u
, UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_DBUS_TARGET
, NULL
, true)) < 0)
1132 if (s
->meta
.default_dependencies
)
1133 if ((r
= service_add_default_dependencies(s
)) < 0)
1137 return service_verify(s
);
1140 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
1142 ServiceExecCommand c
;
1143 Service
*s
= SERVICE(u
);
1144 const char *prefix2
;
1149 p2
= strappend(prefix
, "\t");
1150 prefix2
= p2
? p2
: prefix
;
1153 "%sService State: %s\n"
1154 "%sPermissionsStartOnly: %s\n"
1155 "%sRootDirectoryStartOnly: %s\n"
1156 "%sRemainAfterExit: %s\n"
1157 "%sGuessMainPID: %s\n"
1160 "%sNotifyAccess: %s\n",
1161 prefix
, service_state_to_string(s
->state
),
1162 prefix
, yes_no(s
->permissions_start_only
),
1163 prefix
, yes_no(s
->root_directory_start_only
),
1164 prefix
, yes_no(s
->remain_after_exit
),
1165 prefix
, yes_no(s
->guess_main_pid
),
1166 prefix
, service_type_to_string(s
->type
),
1167 prefix
, service_restart_to_string(s
->restart
),
1168 prefix
, notify_access_to_string(s
->notify_access
));
1170 if (s
->control_pid
> 0)
1172 "%sControl PID: %lu\n",
1173 prefix
, (unsigned long) s
->control_pid
);
1175 if (s
->main_pid
> 0)
1177 "%sMain PID: %lu\n",
1178 prefix
, (unsigned long) s
->main_pid
);
1183 prefix
, s
->pid_file
);
1188 "%sBus Name Good: %s\n",
1189 prefix
, s
->bus_name
,
1190 prefix
, yes_no(s
->bus_name_good
));
1192 exec_context_dump(&s
->exec_context
, f
, prefix
);
1194 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
1196 if (!s
->exec_command
[c
])
1199 fprintf(f
, "%s-> %s:\n",
1200 prefix
, service_exec_command_to_string(c
));
1202 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
1205 #ifdef HAVE_SYSV_COMPAT
1208 "%sSysV Init Script Path: %s\n"
1209 "%sSysV Init Script has LSB Header: %s\n"
1210 "%sSysVEnabled: %s\n",
1211 prefix
, s
->sysv_path
,
1212 prefix
, yes_no(s
->sysv_has_lsb
),
1213 prefix
, yes_no(s
->sysv_enabled
));
1215 if (s
->sysv_start_priority
>= 0)
1217 "%sSysVStartPriority: %i\n",
1218 prefix
, s
->sysv_start_priority
);
1220 if (s
->sysv_runlevels
)
1221 fprintf(f
, "%sSysVRunLevels: %s\n",
1222 prefix
, s
->sysv_runlevels
);
1225 if (s
->fsck_passno
> 0)
1227 "%sFsckPassNo: %i\n",
1228 prefix
, s
->fsck_passno
);
1231 fprintf(f
, "%sStatus Text: %s\n",
1232 prefix
, s
->status_text
);
1237 static int service_load_pid_file(Service
*s
) {
1247 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
1250 r
= parse_pid(k
, &pid
);
1256 if (kill(pid
, 0) < 0 && errno
!= EPERM
) {
1257 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1258 (unsigned long) pid
, s
->pid_file
);
1262 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1265 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1266 /* FIXME: we need to do something here */
1272 static int service_search_main_pid(Service
*s
) {
1278 /* If we know it anyway, don't ever fallback to unreliable
1280 if (s
->main_pid_known
)
1283 if (!s
->guess_main_pid
)
1286 assert(s
->main_pid
<= 0);
1288 if ((pid
= cgroup_bonding_search_main_pid_list(s
->meta
.cgroup_bondings
)) <= 0)
1291 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1294 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1295 /* FIXME: we need to do something here */
1301 static int service_get_sockets(Service
*s
, Set
**_set
) {
1310 if (s
->socket_fd
>= 0)
1313 if (!set_isempty(s
->configured_sockets
))
1316 /* Collects all Socket objects that belong to this
1317 * service. Note that a service might have multiple sockets
1318 * via multiple names. */
1320 if (!(set
= set_new(NULL
, NULL
)))
1323 SET_FOREACH(t
, s
->meta
.names
, i
) {
1327 /* Look for all socket objects that go by any of our
1328 * units and collect their fds */
1330 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
1335 p
= manager_get_unit(s
->meta
.manager
, k
);
1341 if ((r
= set_put(set
, p
)) < 0)
1353 static int service_notify_sockets_dead(Service
*s
) {
1355 Set
*set
, *free_set
= NULL
;
1361 /* Notifies all our sockets when we die */
1363 if (s
->socket_fd
>= 0)
1366 if (!set_isempty(s
->configured_sockets
))
1367 set
= s
->configured_sockets
;
1369 if ((r
= service_get_sockets(s
, &free_set
)) < 0)
1375 SET_FOREACH(sock
, set
, i
)
1376 socket_notify_service_dead(sock
);
1383 static void service_set_state(Service
*s
, ServiceState state
) {
1384 ServiceState old_state
;
1387 old_state
= s
->state
;
1390 if (state
!= SERVICE_START_PRE
&&
1391 state
!= SERVICE_START
&&
1392 state
!= SERVICE_START_POST
&&
1393 state
!= SERVICE_RELOAD
&&
1394 state
!= SERVICE_STOP
&&
1395 state
!= SERVICE_STOP_SIGTERM
&&
1396 state
!= SERVICE_STOP_SIGKILL
&&
1397 state
!= SERVICE_STOP_POST
&&
1398 state
!= SERVICE_FINAL_SIGTERM
&&
1399 state
!= SERVICE_FINAL_SIGKILL
&&
1400 state
!= SERVICE_AUTO_RESTART
)
1401 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1403 if (state
!= SERVICE_START
&&
1404 state
!= SERVICE_START_POST
&&
1405 state
!= SERVICE_RUNNING
&&
1406 state
!= SERVICE_RELOAD
&&
1407 state
!= SERVICE_STOP
&&
1408 state
!= SERVICE_STOP_SIGTERM
&&
1409 state
!= SERVICE_STOP_SIGKILL
) {
1410 service_unwatch_main_pid(s
);
1411 s
->main_command
= NULL
;
1414 if (state
!= SERVICE_START_PRE
&&
1415 state
!= SERVICE_START
&&
1416 state
!= SERVICE_START_POST
&&
1417 state
!= SERVICE_RELOAD
&&
1418 state
!= SERVICE_STOP
&&
1419 state
!= SERVICE_STOP_SIGTERM
&&
1420 state
!= SERVICE_STOP_SIGKILL
&&
1421 state
!= SERVICE_STOP_POST
&&
1422 state
!= SERVICE_FINAL_SIGTERM
&&
1423 state
!= SERVICE_FINAL_SIGKILL
) {
1424 service_unwatch_control_pid(s
);
1425 s
->control_command
= NULL
;
1426 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1429 if (state
== SERVICE_DEAD
||
1430 state
== SERVICE_STOP
||
1431 state
== SERVICE_STOP_SIGTERM
||
1432 state
== SERVICE_STOP_SIGKILL
||
1433 state
== SERVICE_STOP_POST
||
1434 state
== SERVICE_FINAL_SIGTERM
||
1435 state
== SERVICE_FINAL_SIGKILL
||
1436 state
== SERVICE_FAILED
||
1437 state
== SERVICE_AUTO_RESTART
)
1438 service_notify_sockets_dead(s
);
1440 if (state
!= SERVICE_START_PRE
&&
1441 state
!= SERVICE_START
&&
1442 state
!= SERVICE_START_POST
&&
1443 state
!= SERVICE_RUNNING
&&
1444 state
!= SERVICE_RELOAD
&&
1445 state
!= SERVICE_STOP
&&
1446 state
!= SERVICE_STOP_SIGTERM
&&
1447 state
!= SERVICE_STOP_SIGKILL
&&
1448 state
!= SERVICE_STOP_POST
&&
1449 state
!= SERVICE_FINAL_SIGTERM
&&
1450 state
!= SERVICE_FINAL_SIGKILL
&&
1451 !(state
== SERVICE_DEAD
&& s
->meta
.job
)) {
1452 service_close_socket_fd(s
);
1453 service_connection_unref(s
);
1456 /* For the inactive states unit_notify() will trim the cgroup,
1457 * but for exit we have to do that ourselves... */
1458 if (state
== SERVICE_EXITED
)
1459 cgroup_bonding_trim_list(s
->meta
.cgroup_bondings
, true);
1461 if (old_state
!= state
)
1462 log_debug("%s changed %s -> %s", s
->meta
.id
, service_state_to_string(old_state
), service_state_to_string(state
));
1464 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], !s
->reload_failure
);
1465 s
->reload_failure
= false;
1468 static int service_coldplug(Unit
*u
) {
1469 Service
*s
= SERVICE(u
);
1473 assert(s
->state
== SERVICE_DEAD
);
1475 if (s
->deserialized_state
!= s
->state
) {
1477 if (s
->deserialized_state
== SERVICE_START_PRE
||
1478 s
->deserialized_state
== SERVICE_START
||
1479 s
->deserialized_state
== SERVICE_START_POST
||
1480 s
->deserialized_state
== SERVICE_RELOAD
||
1481 s
->deserialized_state
== SERVICE_STOP
||
1482 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1483 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1484 s
->deserialized_state
== SERVICE_STOP_POST
||
1485 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1486 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
||
1487 s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
1489 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
|| s
->timeout_usec
> 0) {
1492 k
= s
->deserialized_state
== SERVICE_AUTO_RESTART
? s
->restart_usec
: s
->timeout_usec
;
1494 if ((r
= unit_watch_timer(UNIT(s
), k
, &s
->timer_watch
)) < 0)
1499 if ((s
->deserialized_state
== SERVICE_START
&&
1500 (s
->type
== SERVICE_FORKING
||
1501 s
->type
== SERVICE_DBUS
||
1502 s
->type
== SERVICE_ONESHOT
||
1503 s
->type
== SERVICE_NOTIFY
)) ||
1504 s
->deserialized_state
== SERVICE_START_POST
||
1505 s
->deserialized_state
== SERVICE_RUNNING
||
1506 s
->deserialized_state
== SERVICE_RELOAD
||
1507 s
->deserialized_state
== SERVICE_STOP
||
1508 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1509 s
->deserialized_state
== SERVICE_STOP_SIGKILL
)
1510 if (s
->main_pid
> 0)
1511 if ((r
= unit_watch_pid(UNIT(s
), s
->main_pid
)) < 0)
1514 if (s
->deserialized_state
== SERVICE_START_PRE
||
1515 s
->deserialized_state
== SERVICE_START
||
1516 s
->deserialized_state
== SERVICE_START_POST
||
1517 s
->deserialized_state
== SERVICE_RELOAD
||
1518 s
->deserialized_state
== SERVICE_STOP
||
1519 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1520 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1521 s
->deserialized_state
== SERVICE_STOP_POST
||
1522 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1523 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
)
1524 if (s
->control_pid
> 0)
1525 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1528 service_set_state(s
, s
->deserialized_state
);
1534 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1538 unsigned rn_fds
= 0;
1539 Set
*set
, *free_set
= NULL
;
1546 if (s
->socket_fd
>= 0)
1549 if (!set_isempty(s
->configured_sockets
))
1550 set
= s
->configured_sockets
;
1552 if ((r
= service_get_sockets(s
, &free_set
)) < 0)
1558 SET_FOREACH(sock
, set
, i
) {
1562 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1574 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1580 memcpy(t
, rfds
, rn_fds
);
1581 memcpy(t
+rn_fds
, cfds
, cn_fds
);
1586 rn_fds
= rn_fds
+cn_fds
;
1604 static int service_spawn(
1609 bool apply_permissions
,
1611 bool apply_tty_stdin
,
1612 bool set_notify_socket
,
1617 int *fds
= NULL
, *fdsbuf
= NULL
;
1618 unsigned n_fds
= 0, n_env
= 0;
1619 char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
;
1626 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1627 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1628 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1630 if (s
->socket_fd
>= 0) {
1631 fds
= &s
->socket_fd
;
1634 if ((r
= service_collect_fds(s
, &fdsbuf
, &n_fds
)) < 0)
1641 if (timeout
&& s
->timeout_usec
) {
1642 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1645 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1647 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1652 if (!(our_env
= new0(char*, 4))) {
1657 if (set_notify_socket
)
1658 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=@%s", s
->meta
.manager
->notify_socket
) < 0) {
1663 if (s
->main_pid
> 0)
1664 if (asprintf(our_env
+ n_env
++, "MAINPID=%lu", (unsigned long) s
->main_pid
) < 0) {
1669 if (!(final_env
= strv_env_merge(2,
1670 s
->meta
.manager
->environment
,
1685 s
->meta
.manager
->confirm_spawn
,
1686 s
->meta
.cgroup_bondings
,
1693 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1694 /* FIXME: we need to do something here */
1700 strv_free(final_env
);
1710 strv_free(final_env
);
1713 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1718 static int main_pid_good(Service
*s
) {
1721 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1724 /* If we know the pid file, then lets just check if it is
1726 if (s
->main_pid_known
)
1727 return s
->main_pid
> 0;
1729 /* We don't know the pid */
1733 static int control_pid_good(Service
*s
) {
1736 return s
->control_pid
> 0;
1739 static int cgroup_good(Service
*s
) {
1744 if ((r
= cgroup_bonding_is_empty_list(s
->meta
.cgroup_bondings
)) < 0)
1750 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1757 if (allow_restart
&&
1758 !s
->forbid_restart
&&
1759 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1760 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
) ||
1761 (s
->restart
== SERVICE_RESTART_ON_FAILURE
&& s
->failure
) ||
1762 (s
->restart
== SERVICE_RESTART_ON_ABORT
&& s
->failure
&&
1763 (s
->main_exec_status
.code
== CLD_KILLED
||
1764 s
->main_exec_status
.code
== CLD_DUMPED
)))) {
1766 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1769 service_set_state(s
, SERVICE_AUTO_RESTART
);
1771 service_set_state(s
, s
->failure
? SERVICE_FAILED
: SERVICE_DEAD
);
1773 s
->forbid_restart
= false;
1778 log_warning("%s failed to run install restart timer: %s", s
->meta
.id
, strerror(-r
));
1779 service_enter_dead(s
, false, false);
1782 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1784 static void service_enter_stop_post(Service
*s
, bool success
) {
1791 service_unwatch_control_pid(s
);
1793 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1794 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1796 if ((r
= service_spawn(s
,
1800 !s
->permissions_start_only
,
1801 !s
->root_directory_start_only
,
1804 &s
->control_pid
)) < 0)
1808 service_set_state(s
, SERVICE_STOP_POST
);
1810 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, true);
1815 log_warning("%s failed to run 'stop-post' task: %s", s
->meta
.id
, strerror(-r
));
1816 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1819 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1821 Set
*pid_set
= NULL
;
1822 bool wait_for_exit
= false;
1829 if (s
->exec_context
.kill_mode
!= KILL_NONE
) {
1830 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? s
->exec_context
.kill_signal
: SIGKILL
;
1832 if (s
->main_pid
> 0) {
1833 if (kill(s
->exec_context
.kill_mode
== KILL_PROCESS_GROUP
?
1835 s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1837 log_warning("Failed to kill main process %li: %m", (long) s
->main_pid
);
1839 wait_for_exit
= true;
1842 if (s
->control_pid
> 0) {
1843 if (kill(s
->exec_context
.kill_mode
== KILL_PROCESS_GROUP
?
1845 s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1847 log_warning("Failed to kill control process %li: %m", (long) s
->control_pid
);
1849 wait_for_exit
= true;
1852 if (s
->exec_context
.kill_mode
== KILL_CONTROL_GROUP
) {
1854 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
))) {
1859 /* Exclude the main/control pids from being killed via the cgroup */
1860 if (s
->main_pid
> 0)
1861 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->main_pid
))) < 0)
1864 if (s
->control_pid
> 0)
1865 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0)
1868 if ((r
= cgroup_bonding_kill_list(s
->meta
.cgroup_bondings
, sig
, pid_set
)) < 0) {
1869 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
1870 log_warning("Failed to kill control group: %s", strerror(-r
));
1872 wait_for_exit
= true;
1878 if (wait_for_exit
) {
1879 if (s
->timeout_usec
> 0)
1880 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1883 service_set_state(s
, state
);
1884 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1885 service_enter_stop_post(s
, true);
1887 service_enter_dead(s
, true, true);
1892 log_warning("%s failed to kill processes: %s", s
->meta
.id
, strerror(-r
));
1894 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1895 service_enter_stop_post(s
, false);
1897 service_enter_dead(s
, false, true);
1903 static void service_enter_stop(Service
*s
, bool success
) {
1911 service_unwatch_control_pid(s
);
1913 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
1914 s
->control_command_id
= SERVICE_EXEC_STOP
;
1916 if ((r
= service_spawn(s
,
1920 !s
->permissions_start_only
,
1921 !s
->root_directory_start_only
,
1924 &s
->control_pid
)) < 0)
1927 service_set_state(s
, SERVICE_STOP
);
1929 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1934 log_warning("%s failed to run 'stop' task: %s", s
->meta
.id
, strerror(-r
));
1935 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1938 static void service_enter_running(Service
*s
, bool success
) {
1939 int main_pid_ok
, cgroup_ok
;
1945 main_pid_ok
= main_pid_good(s
);
1946 cgroup_ok
= cgroup_good(s
);
1948 if ((main_pid_ok
> 0 || (main_pid_ok
< 0 && cgroup_ok
!= 0)) &&
1949 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
1950 service_set_state(s
, SERVICE_RUNNING
);
1951 else if (s
->remain_after_exit
)
1952 service_set_state(s
, SERVICE_EXITED
);
1954 service_enter_stop(s
, true);
1957 static void service_enter_start_post(Service
*s
) {
1961 service_unwatch_control_pid(s
);
1963 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
1964 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1966 if ((r
= service_spawn(s
,
1970 !s
->permissions_start_only
,
1971 !s
->root_directory_start_only
,
1974 &s
->control_pid
)) < 0)
1977 service_set_state(s
, SERVICE_START_POST
);
1979 service_enter_running(s
, true);
1984 log_warning("%s failed to run 'start-post' task: %s", s
->meta
.id
, strerror(-r
));
1985 service_enter_stop(s
, false);
1988 static void service_enter_start(Service
*s
) {
1995 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1996 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
|| s
->type
== SERVICE_ONESHOT
);
1998 if (s
->type
== SERVICE_FORKING
)
1999 service_unwatch_control_pid(s
);
2001 service_unwatch_main_pid(s
);
2003 if (s
->type
== SERVICE_FORKING
) {
2004 s
->control_command_id
= SERVICE_EXEC_START
;
2005 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
2007 s
->main_command
= NULL
;
2009 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2010 s
->control_command
= NULL
;
2012 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
2015 if ((r
= service_spawn(s
,
2017 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
,
2022 s
->notify_access
!= NOTIFY_NONE
,
2026 if (s
->type
== SERVICE_SIMPLE
) {
2027 /* For simple services we immediately start
2028 * the START_POST binaries. */
2030 service_set_main_pid(s
, pid
);
2031 service_enter_start_post(s
);
2033 } else if (s
->type
== SERVICE_FORKING
) {
2035 /* For forking services we wait until the start
2036 * process exited. */
2038 s
->control_pid
= pid
;
2039 service_set_state(s
, SERVICE_START
);
2041 } else if (s
->type
== SERVICE_ONESHOT
||
2042 s
->type
== SERVICE_DBUS
||
2043 s
->type
== SERVICE_NOTIFY
) {
2045 /* For oneshot services we wait until the start
2046 * process exited, too, but it is our main process. */
2048 /* For D-Bus services we know the main pid right away,
2049 * but wait for the bus name to appear on the
2050 * bus. Notify services are similar. */
2052 service_set_main_pid(s
, pid
);
2053 service_set_state(s
, SERVICE_START
);
2055 assert_not_reached("Unknown service type");
2060 log_warning("%s failed to run 'start' task: %s", s
->meta
.id
, strerror(-r
));
2061 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2064 static void service_enter_start_pre(Service
*s
) {
2069 service_unwatch_control_pid(s
);
2071 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
2072 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
2074 if ((r
= service_spawn(s
,
2078 !s
->permissions_start_only
,
2079 !s
->root_directory_start_only
,
2082 &s
->control_pid
)) < 0)
2085 service_set_state(s
, SERVICE_START_PRE
);
2087 service_enter_start(s
);
2092 log_warning("%s failed to run 'start-pre' task: %s", s
->meta
.id
, strerror(-r
));
2093 service_enter_dead(s
, false, true);
2096 static void service_enter_restart(Service
*s
) {
2101 dbus_error_init(&error
);
2104 log_info("Job pending for unit, delaying automatic restart.");
2106 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
2110 service_enter_dead(s
, true, false);
2112 if ((r
= manager_add_job(s
->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
)) < 0)
2115 log_debug("%s scheduled restart job.", s
->meta
.id
);
2119 log_warning("%s failed to schedule restart job: %s", s
->meta
.id
, bus_error(&error
, -r
));
2120 service_enter_dead(s
, false, false);
2122 dbus_error_free(&error
);
2125 static void service_enter_reload(Service
*s
) {
2130 service_unwatch_control_pid(s
);
2132 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
2133 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
2135 if ((r
= service_spawn(s
,
2139 !s
->permissions_start_only
,
2140 !s
->root_directory_start_only
,
2143 &s
->control_pid
)) < 0)
2146 service_set_state(s
, SERVICE_RELOAD
);
2148 service_enter_running(s
, true);
2153 log_warning("%s failed to run 'reload' task: %s", s
->meta
.id
, strerror(-r
));
2154 s
->reload_failure
= true;
2155 service_enter_running(s
, true);
2158 static void service_run_next_control(Service
*s
, bool success
) {
2162 assert(s
->control_command
);
2163 assert(s
->control_command
->command_next
);
2168 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
2170 s
->control_command
= s
->control_command
->command_next
;
2171 service_unwatch_control_pid(s
);
2173 if ((r
= service_spawn(s
,
2177 !s
->permissions_start_only
,
2178 !s
->root_directory_start_only
,
2179 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
2180 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
2182 &s
->control_pid
)) < 0)
2188 log_warning("%s failed to run next control task: %s", s
->meta
.id
, strerror(-r
));
2190 if (s
->state
== SERVICE_START_PRE
)
2191 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2192 else if (s
->state
== SERVICE_STOP
)
2193 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
2194 else if (s
->state
== SERVICE_STOP_POST
)
2195 service_enter_dead(s
, false, true);
2196 else if (s
->state
== SERVICE_RELOAD
) {
2197 s
->reload_failure
= true;
2198 service_enter_running(s
, true);
2200 service_enter_stop(s
, false);
2203 static void service_run_next_main(Service
*s
, bool success
) {
2208 assert(s
->main_command
);
2209 assert(s
->main_command
->command_next
);
2210 assert(s
->type
== SERVICE_ONESHOT
);
2215 s
->main_command
= s
->main_command
->command_next
;
2216 service_unwatch_main_pid(s
);
2218 if ((r
= service_spawn(s
,
2225 s
->notify_access
!= NOTIFY_NONE
,
2229 service_set_main_pid(s
, pid
);
2234 log_warning("%s failed to run next main task: %s", s
->meta
.id
, strerror(-r
));
2235 service_enter_stop(s
, false);
2238 static int service_start(Unit
*u
) {
2239 Service
*s
= SERVICE(u
);
2243 /* We cannot fulfill this request right now, try again later
2245 if (s
->state
== SERVICE_STOP
||
2246 s
->state
== SERVICE_STOP_SIGTERM
||
2247 s
->state
== SERVICE_STOP_SIGKILL
||
2248 s
->state
== SERVICE_STOP_POST
||
2249 s
->state
== SERVICE_FINAL_SIGTERM
||
2250 s
->state
== SERVICE_FINAL_SIGKILL
)
2253 /* Already on it! */
2254 if (s
->state
== SERVICE_START_PRE
||
2255 s
->state
== SERVICE_START
||
2256 s
->state
== SERVICE_START_POST
)
2259 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_FAILED
|| s
->state
== SERVICE_AUTO_RESTART
);
2261 /* Make sure we don't enter a busy loop of some kind. */
2262 if (!ratelimit_test(&s
->ratelimit
)) {
2263 log_warning("%s start request repeated too quickly, refusing to start.", u
->meta
.id
);
2268 s
->main_pid_known
= false;
2269 s
->forbid_restart
= false;
2271 service_enter_start_pre(s
);
2275 static int service_stop(Unit
*u
) {
2276 Service
*s
= SERVICE(u
);
2280 /* This is a user request, so don't do restarts on this
2282 s
->forbid_restart
= true;
2285 if (s
->state
== SERVICE_STOP
||
2286 s
->state
== SERVICE_STOP_SIGTERM
||
2287 s
->state
== SERVICE_STOP_SIGKILL
||
2288 s
->state
== SERVICE_STOP_POST
||
2289 s
->state
== SERVICE_FINAL_SIGTERM
||
2290 s
->state
== SERVICE_FINAL_SIGKILL
)
2293 /* Don't allow a restart */
2294 if (s
->state
== SERVICE_AUTO_RESTART
) {
2295 service_set_state(s
, SERVICE_DEAD
);
2299 /* If there's already something running we go directly into
2301 if (s
->state
== SERVICE_START_PRE
||
2302 s
->state
== SERVICE_START
||
2303 s
->state
== SERVICE_START_POST
||
2304 s
->state
== SERVICE_RELOAD
) {
2305 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
2309 assert(s
->state
== SERVICE_RUNNING
||
2310 s
->state
== SERVICE_EXITED
);
2312 service_enter_stop(s
, true);
2316 static int service_reload(Unit
*u
) {
2317 Service
*s
= SERVICE(u
);
2321 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2323 service_enter_reload(s
);
2327 static bool service_can_reload(Unit
*u
) {
2328 Service
*s
= SERVICE(u
);
2332 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2335 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2336 Service
*s
= SERVICE(u
);
2342 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2343 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
2345 if (s
->control_pid
> 0)
2346 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
2348 if (s
->main_pid_known
&& s
->main_pid
> 0)
2349 unit_serialize_item_format(u
, f
, "main-pid", "%lu", (unsigned long) s
->main_pid
);
2351 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2354 unit_serialize_item(u
, f
, "status-text", s
->status_text
);
2356 /* There's a minor uncleanliness here: if there are multiple
2357 * commands attached here, we will start from the first one
2359 if (s
->control_command_id
>= 0)
2360 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2362 if (s
->socket_fd
>= 0) {
2365 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
2368 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
2371 if (s
->main_exec_status
.pid
> 0) {
2372 unit_serialize_item_format(u
, f
, "main-exec-status-pid", "%lu", (unsigned long) s
->main_exec_status
.pid
);
2373 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2374 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2376 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2377 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2378 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2385 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2386 Service
*s
= SERVICE(u
);
2393 if (streq(key
, "state")) {
2396 if ((state
= service_state_from_string(value
)) < 0)
2397 log_debug("Failed to parse state value %s", value
);
2399 s
->deserialized_state
= state
;
2400 } else if (streq(key
, "failure")) {
2403 if ((b
= parse_boolean(value
)) < 0)
2404 log_debug("Failed to parse failure value %s", value
);
2406 s
->failure
= b
|| s
->failure
;
2407 } else if (streq(key
, "control-pid")) {
2410 if (parse_pid(value
, &pid
) < 0)
2411 log_debug("Failed to parse control-pid value %s", value
);
2413 s
->control_pid
= pid
;
2414 } else if (streq(key
, "main-pid")) {
2417 if (parse_pid(value
, &pid
) < 0)
2418 log_debug("Failed to parse main-pid value %s", value
);
2420 service_set_main_pid(s
, (pid_t
) pid
);
2421 } else if (streq(key
, "main-pid-known")) {
2424 if ((b
= parse_boolean(value
)) < 0)
2425 log_debug("Failed to parse main-pid-known value %s", value
);
2427 s
->main_pid_known
= b
;
2428 } else if (streq(key
, "status-text")) {
2431 if ((t
= strdup(value
))) {
2432 free(s
->status_text
);
2436 } else if (streq(key
, "control-command")) {
2437 ServiceExecCommand id
;
2439 if ((id
= service_exec_command_from_string(value
)) < 0)
2440 log_debug("Failed to parse exec-command value %s", value
);
2442 s
->control_command_id
= id
;
2443 s
->control_command
= s
->exec_command
[id
];
2445 } else if (streq(key
, "socket-fd")) {
2448 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2449 log_debug("Failed to parse socket-fd value %s", value
);
2452 if (s
->socket_fd
>= 0)
2453 close_nointr_nofail(s
->socket_fd
);
2454 s
->socket_fd
= fdset_remove(fds
, fd
);
2456 } else if (streq(key
, "main-exec-status-pid")) {
2459 if (parse_pid(value
, &pid
) < 0)
2460 log_debug("Failed to parse main-exec-status-pid value %s", value
);
2462 s
->main_exec_status
.pid
= pid
;
2463 } else if (streq(key
, "main-exec-status-code")) {
2466 if (safe_atoi(value
, &i
) < 0)
2467 log_debug("Failed to parse main-exec-status-code value %s", value
);
2469 s
->main_exec_status
.code
= i
;
2470 } else if (streq(key
, "main-exec-status-status")) {
2473 if (safe_atoi(value
, &i
) < 0)
2474 log_debug("Failed to parse main-exec-status-status value %s", value
);
2476 s
->main_exec_status
.status
= i
;
2477 } else if (streq(key
, "main-exec-status-start"))
2478 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2479 else if (streq(key
, "main-exec-status-exit"))
2480 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2482 log_debug("Unknown serialization key '%s'", key
);
2487 static UnitActiveState
service_active_state(Unit
*u
) {
2490 return state_translation_table
[SERVICE(u
)->state
];
2493 static const char *service_sub_state_to_string(Unit
*u
) {
2496 return service_state_to_string(SERVICE(u
)->state
);
2499 static bool service_check_gc(Unit
*u
) {
2500 Service
*s
= SERVICE(u
);
2504 /* Never clean up services that still have a process around,
2505 * even if the service is formally dead. */
2506 if (cgroup_good(s
) > 0 ||
2507 main_pid_good(s
) > 0 ||
2508 control_pid_good(s
) > 0)
2511 #ifdef HAVE_SYSV_COMPAT
2519 static bool service_check_snapshot(Unit
*u
) {
2520 Service
*s
= SERVICE(u
);
2524 return !s
->got_socket_fd
;
2527 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2528 Service
*s
= SERVICE(u
);
2534 if (!s
->meta
.fragment_path
)
2535 success
= is_clean_exit_lsb(code
, status
);
2537 success
= is_clean_exit(code
, status
);
2539 if (s
->main_pid
== pid
) {
2542 exec_status_exit(&s
->main_exec_status
, pid
, code
, status
, s
->exec_context
.utmp_id
);
2544 /* If this is not a forking service than the main
2545 * process got started and hence we copy the exit
2546 * status so that it is recorded both as main and as
2547 * control process exit status */
2548 if (s
->main_command
) {
2549 s
->main_command
->exec_status
= s
->main_exec_status
;
2551 if (s
->main_command
->ignore
)
2555 log_full(success
? LOG_DEBUG
: LOG_NOTICE
,
2556 "%s: main process exited, code=%s, status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
2557 s
->failure
= s
->failure
|| !success
;
2559 if (s
->main_command
&&
2560 s
->main_command
->command_next
&&
2563 /* There is another command to *
2564 * execute, so let's do that. */
2566 log_debug("%s running next main command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2567 service_run_next_main(s
, success
);
2571 /* The service exited, so the service is officially
2573 s
->main_command
= NULL
;
2577 case SERVICE_START_POST
:
2578 case SERVICE_RELOAD
:
2580 /* Need to wait until the operation is
2585 if (s
->type
== SERVICE_ONESHOT
) {
2586 /* This was our main goal, so let's go on */
2588 service_enter_start_post(s
);
2590 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2593 assert(s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
);
2598 case SERVICE_RUNNING
:
2599 service_enter_running(s
, success
);
2602 case SERVICE_STOP_SIGTERM
:
2603 case SERVICE_STOP_SIGKILL
:
2605 if (!control_pid_good(s
))
2606 service_enter_stop_post(s
, success
);
2608 /* If there is still a control process, wait for that first */
2612 assert_not_reached("Uh, main process died at wrong time.");
2616 } else if (s
->control_pid
== pid
) {
2620 if (s
->control_command
) {
2621 exec_status_exit(&s
->control_command
->exec_status
, pid
, code
, status
, s
->exec_context
.utmp_id
);
2623 if (s
->control_command
->ignore
)
2627 log_full(success
? LOG_DEBUG
: LOG_NOTICE
,
2628 "%s: control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
2629 s
->failure
= s
->failure
|| !success
;
2631 if (s
->control_command
&&
2632 s
->control_command
->command_next
&&
2635 /* There is another command to *
2636 * execute, so let's do that. */
2638 log_debug("%s running next control command for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2639 service_run_next_control(s
, success
);
2642 /* No further commands for this step, so let's
2643 * figure out what to do next */
2645 s
->control_command
= NULL
;
2646 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2648 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, service_state_to_string(s
->state
));
2652 case SERVICE_START_PRE
:
2654 service_enter_start(s
);
2656 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2660 assert(s
->type
== SERVICE_FORKING
);
2662 /* Let's try to load the pid
2663 * file here if we can. We
2664 * ignore the return value,
2665 * since the PID file might
2666 * actually be created by a
2667 * START_POST script */
2670 service_load_pid_file(s
);
2671 service_search_main_pid(s
);
2673 service_enter_start_post(s
);
2675 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2679 case SERVICE_START_POST
:
2680 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
2683 /* Hmm, let's see if we can
2684 * load the pid now after the
2685 * start-post scripts got
2688 if ((r
= service_load_pid_file(s
)) < 0)
2689 log_warning("%s: failed to load PID file %s: %s", s
->meta
.id
, s
->pid_file
, strerror(-r
));
2692 s
->reload_failure
= !success
;
2693 service_enter_running(s
, true);
2696 case SERVICE_RELOAD
:
2698 service_load_pid_file(s
);
2699 service_search_main_pid(s
);
2702 s
->reload_failure
= !success
;
2703 service_enter_running(s
, true);
2707 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
2710 case SERVICE_STOP_SIGTERM
:
2711 case SERVICE_STOP_SIGKILL
:
2712 if (main_pid_good(s
) <= 0)
2713 service_enter_stop_post(s
, success
);
2715 /* If there is still a service
2716 * process around, wait until
2717 * that one quit, too */
2720 case SERVICE_STOP_POST
:
2721 case SERVICE_FINAL_SIGTERM
:
2722 case SERVICE_FINAL_SIGKILL
:
2723 service_enter_dead(s
, success
, true);
2727 assert_not_reached("Uh, control process died at wrong time.");
2732 /* Notify clients about changed exit status */
2733 unit_add_to_dbus_queue(u
);
2736 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
2737 Service
*s
= SERVICE(u
);
2740 assert(elapsed
== 1);
2742 assert(w
== &s
->timer_watch
);
2746 case SERVICE_START_PRE
:
2748 log_warning("%s operation timed out. Terminating.", u
->meta
.id
);
2749 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2752 case SERVICE_START_POST
:
2753 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
2754 service_enter_stop(s
, false);
2757 case SERVICE_RELOAD
:
2758 log_warning("%s operation timed out. Stopping.", u
->meta
.id
);
2759 s
->reload_failure
= true;
2760 service_enter_running(s
, true);
2764 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
2765 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
2768 case SERVICE_STOP_SIGTERM
:
2769 if (s
->exec_context
.send_sigkill
) {
2770 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
2771 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
2773 log_warning("%s stopping timed out. Skipping SIGKILL.", u
->meta
.id
);
2774 service_enter_stop_post(s
, false);
2779 case SERVICE_STOP_SIGKILL
:
2780 /* Uh, wie sent a SIGKILL and it is still not gone?
2781 * Must be something we cannot kill, so let's just be
2782 * weirded out and continue */
2784 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
2785 service_enter_stop_post(s
, false);
2788 case SERVICE_STOP_POST
:
2789 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
2790 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
2793 case SERVICE_FINAL_SIGTERM
:
2794 if (s
->exec_context
.send_sigkill
) {
2795 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
2796 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
2798 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u
->meta
.id
);
2799 service_enter_dead(s
, false, true);
2804 case SERVICE_FINAL_SIGKILL
:
2805 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u
->meta
.id
);
2806 service_enter_dead(s
, false, true);
2809 case SERVICE_AUTO_RESTART
:
2810 log_info("%s holdoff time over, scheduling restart.", u
->meta
.id
);
2811 service_enter_restart(s
);
2815 assert_not_reached("Timeout at wrong time.");
2819 static void service_cgroup_notify_event(Unit
*u
) {
2820 Service
*s
= SERVICE(u
);
2824 log_debug("%s: cgroup is empty", u
->meta
.id
);
2828 /* Waiting for SIGCHLD is usually more interesting,
2829 * because it includes return codes/signals. Which is
2830 * why we ignore the cgroup events for most cases,
2831 * except when we don't know pid which to expect the
2834 case SERVICE_RUNNING
:
2835 service_enter_running(s
, true);
2838 case SERVICE_STOP_SIGTERM
:
2839 case SERVICE_STOP_SIGKILL
:
2840 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2841 service_enter_stop_post(s
, true);
2845 case SERVICE_FINAL_SIGTERM
:
2846 case SERVICE_FINAL_SIGKILL
:
2847 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2848 service_enter_dead(s
, true, true);
2857 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
) {
2858 Service
*s
= SERVICE(u
);
2863 if (s
->notify_access
== NOTIFY_NONE
) {
2864 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2865 u
->meta
.id
, (unsigned long) pid
);
2869 if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2870 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2871 u
->meta
.id
, (unsigned long) pid
, (unsigned long) s
->main_pid
);
2875 log_debug("%s: Got message", u
->meta
.id
);
2877 /* Interpret MAINPID= */
2878 if ((e
= strv_find_prefix(tags
, "MAINPID=")) &&
2879 (s
->state
== SERVICE_START
||
2880 s
->state
== SERVICE_START_POST
||
2881 s
->state
== SERVICE_RUNNING
||
2882 s
->state
== SERVICE_RELOAD
)) {
2884 if (parse_pid(e
+ 8, &pid
) < 0)
2885 log_warning("Failed to parse notification message %s", e
);
2887 log_debug("%s: got %s", u
->meta
.id
, e
);
2888 service_set_main_pid(s
, pid
);
2892 /* Interpret READY= */
2893 if (s
->type
== SERVICE_NOTIFY
&&
2894 s
->state
== SERVICE_START
&&
2895 strv_find(tags
, "READY=1")) {
2896 log_debug("%s: got READY=1", u
->meta
.id
);
2898 service_enter_start_post(s
);
2901 /* Interpret STATUS= */
2902 if ((e
= strv_find_prefix(tags
, "STATUS="))) {
2906 if (!(t
= strdup(e
+7))) {
2907 log_error("Failed to allocate string.");
2911 log_debug("%s: got %s", u
->meta
.id
, e
);
2913 free(s
->status_text
);
2916 free(s
->status_text
);
2917 s
->status_text
= NULL
;
2922 /* Notify clients about changed status or main pid */
2923 unit_add_to_dbus_queue(u
);
2926 #ifdef HAVE_SYSV_COMPAT
2927 static int service_enumerate(Manager
*m
) {
2931 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
2932 Set
*runlevel_services
[ELEMENTSOF(rcnd_table
)], *shutdown_services
= NULL
;
2939 zero(runlevel_services
);
2941 STRV_FOREACH(p
, m
->lookup_paths
.sysvrcnd_path
)
2942 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++) {
2947 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
].path
) < 0) {
2955 if (!(d
= opendir(path
))) {
2956 if (errno
!= ENOENT
)
2957 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
2962 while ((de
= readdir(d
))) {
2965 if (ignore_file(de
->d_name
))
2968 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
2971 if (strlen(de
->d_name
) < 4)
2974 a
= undecchar(de
->d_name
[1]);
2975 b
= undecchar(de
->d_name
[2]);
2982 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
].path
, de
->d_name
) < 0) {
2987 if (access(fpath
, X_OK
) < 0) {
2989 if (errno
!= ENOENT
)
2990 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
2996 if (!(name
= sysv_translate_name(de
->d_name
+ 3))) {
3001 if ((r
= manager_load_unit_prepare(m
, name
, NULL
, NULL
, &service
)) < 0) {
3002 log_warning("Failed to prepare unit %s: %s", name
, strerror(-r
));
3006 if (de
->d_name
[0] == 'S') {
3008 if (rcnd_table
[i
].type
== RUNLEVEL_UP
|| rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
) {
3009 SERVICE(service
)->sysv_start_priority
=
3010 MAX(a
*10 + b
, SERVICE(service
)->sysv_start_priority
);
3012 SERVICE(service
)->sysv_enabled
= true;
3015 if ((r
= set_ensure_allocated(&runlevel_services
[i
], trivial_hash_func
, trivial_compare_func
)) < 0)
3018 if ((r
= set_put(runlevel_services
[i
], service
)) < 0)
3021 } else if (de
->d_name
[0] == 'K' &&
3022 (rcnd_table
[i
].type
== RUNLEVEL_DOWN
||
3023 rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
)) {
3025 if ((r
= set_ensure_allocated(&shutdown_services
, trivial_hash_func
, trivial_compare_func
)) < 0)
3028 if ((r
= set_put(shutdown_services
, service
)) < 0)
3034 /* Now we loaded all stubs and are aware of the lowest
3035 start-up priority for all services, not let's actually load
3036 the services, this will also tell us which services are
3037 actually native now */
3038 manager_dispatch_load_queue(m
);
3040 /* If this is a native service, rely on native ways to pull in
3041 * a service, don't pull it in via sysv rcN.d links. */
3042 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
3043 SET_FOREACH(service
, runlevel_services
[i
], j
) {
3044 service
= unit_follow_merge(service
);
3046 if (service
->meta
.fragment_path
)
3049 if ((r
= unit_add_two_dependencies_by_name_inverse(service
, UNIT_AFTER
, UNIT_WANTS
, rcnd_table
[i
].target
, NULL
, true)) < 0)
3053 /* We honour K links only for halt/reboot. For the normal
3054 * runlevels we assume the stop jobs will be implicitly added
3055 * by the core logic. Also, we don't really distuingish here
3056 * between the runlevels 0 and 6 and just add them to the
3057 * special shutdown target. On SUSE the boot.d/ runlevel is
3058 * also used for shutdown, so we add links for that too to the
3059 * shutdown target.*/
3060 SET_FOREACH(service
, shutdown_services
, j
) {
3061 service
= unit_follow_merge(service
);
3063 if (service
->meta
.fragment_path
)
3066 if ((r
= unit_add_two_dependencies_by_name(service
, UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true)) < 0)
3077 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
3078 set_free(runlevel_services
[i
]);
3079 set_free(shutdown_services
);
3088 static void service_bus_name_owner_change(
3091 const char *old_owner
,
3092 const char *new_owner
) {
3094 Service
*s
= SERVICE(u
);
3099 assert(streq(s
->bus_name
, name
));
3100 assert(old_owner
|| new_owner
);
3102 if (old_owner
&& new_owner
)
3103 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->meta
.id
, name
, old_owner
, new_owner
);
3105 log_debug("%s's D-Bus name %s no longer registered by %s", u
->meta
.id
, name
, old_owner
);
3107 log_debug("%s's D-Bus name %s now registered by %s", u
->meta
.id
, name
, new_owner
);
3109 s
->bus_name_good
= !!new_owner
;
3111 if (s
->type
== SERVICE_DBUS
) {
3113 /* service_enter_running() will figure out what to
3115 if (s
->state
== SERVICE_RUNNING
)
3116 service_enter_running(s
, true);
3117 else if (s
->state
== SERVICE_START
&& new_owner
)
3118 service_enter_start_post(s
);
3120 } else if (new_owner
&&
3122 (s
->state
== SERVICE_START
||
3123 s
->state
== SERVICE_START_POST
||
3124 s
->state
== SERVICE_RUNNING
||
3125 s
->state
== SERVICE_RELOAD
)) {
3127 /* Try to acquire PID from bus service */
3128 log_debug("Trying to acquire PID from D-Bus name...");
3130 bus_query_pid(u
->meta
.manager
, name
);
3134 static void service_bus_query_pid_done(
3139 Service
*s
= SERVICE(u
);
3144 log_debug("%s's D-Bus name %s is now owned by process %u", u
->meta
.id
, name
, (unsigned) pid
);
3146 if (s
->main_pid
<= 0 &&
3147 (s
->state
== SERVICE_START
||
3148 s
->state
== SERVICE_START_POST
||
3149 s
->state
== SERVICE_RUNNING
||
3150 s
->state
== SERVICE_RELOAD
))
3151 service_set_main_pid(s
, pid
);
3154 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
) {
3158 /* This is called by the socket code when instantiating a new
3159 * service for a stream socket and the socket needs to be
3162 if (s
->meta
.load_state
!= UNIT_LOADED
)
3165 if (s
->socket_fd
>= 0)
3168 if (s
->state
!= SERVICE_DEAD
)
3172 s
->got_socket_fd
= true;
3173 s
->accept_socket
= sock
;
3178 static void service_reset_failed(Unit
*u
) {
3179 Service
*s
= SERVICE(u
);
3183 if (s
->state
== SERVICE_FAILED
)
3184 service_set_state(s
, SERVICE_DEAD
);
3189 static int service_kill(Unit
*u
, KillWho who
, KillMode mode
, int signo
, DBusError
*error
) {
3190 Service
*s
= SERVICE(u
);
3192 Set
*pid_set
= NULL
;
3196 if (s
->main_pid
<= 0 && who
== KILL_MAIN
) {
3197 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No main process to kill");
3201 if (s
->control_pid
<= 0 && who
== KILL_CONTROL
) {
3202 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No control process to kill");
3206 if (s
->control_pid
> 0)
3207 if (kill(mode
== KILL_PROCESS_GROUP
? -s
->control_pid
: s
->control_pid
, signo
) < 0)
3210 if (s
->main_pid
> 0)
3211 if (kill(mode
== KILL_PROCESS_GROUP
? -s
->main_pid
: s
->main_pid
, signo
) < 0)
3214 if (mode
== KILL_CONTROL_GROUP
) {
3217 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
)))
3220 /* Exclude the control/main pid from being killed via the cgroup */
3221 if (s
->control_pid
> 0)
3222 if ((q
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0) {
3227 if (s
->main_pid
> 0)
3228 if ((q
= set_put(pid_set
, LONG_TO_PTR(s
->main_pid
))) < 0) {
3233 if ((q
= cgroup_bonding_kill_list(s
->meta
.cgroup_bondings
, signo
, pid_set
)) < 0)
3234 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
3245 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
3246 [SERVICE_DEAD
] = "dead",
3247 [SERVICE_START_PRE
] = "start-pre",
3248 [SERVICE_START
] = "start",
3249 [SERVICE_START_POST
] = "start-post",
3250 [SERVICE_RUNNING
] = "running",
3251 [SERVICE_EXITED
] = "exited",
3252 [SERVICE_RELOAD
] = "reload",
3253 [SERVICE_STOP
] = "stop",
3254 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
3255 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
3256 [SERVICE_STOP_POST
] = "stop-post",
3257 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
3258 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
3259 [SERVICE_FAILED
] = "failed",
3260 [SERVICE_AUTO_RESTART
] = "auto-restart",
3263 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
3265 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3266 [SERVICE_RESTART_NO
] = "no",
3267 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3268 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3269 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3270 [SERVICE_RESTART_ALWAYS
] = "always"
3273 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3275 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3276 [SERVICE_SIMPLE
] = "simple",
3277 [SERVICE_FORKING
] = "forking",
3278 [SERVICE_ONESHOT
] = "oneshot",
3279 [SERVICE_DBUS
] = "dbus",
3280 [SERVICE_NOTIFY
] = "notify"
3283 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3285 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3286 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3287 [SERVICE_EXEC_START
] = "ExecStart",
3288 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3289 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3290 [SERVICE_EXEC_STOP
] = "ExecStop",
3291 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3294 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3296 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3297 [NOTIFY_NONE
] = "none",
3298 [NOTIFY_MAIN
] = "main",
3299 [NOTIFY_ALL
] = "all"
3302 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3304 const UnitVTable service_vtable
= {
3305 .suffix
= ".service",
3306 .show_status
= true,
3308 .init
= service_init
,
3309 .done
= service_done
,
3310 .load
= service_load
,
3312 .coldplug
= service_coldplug
,
3314 .dump
= service_dump
,
3316 .start
= service_start
,
3317 .stop
= service_stop
,
3318 .reload
= service_reload
,
3320 .can_reload
= service_can_reload
,
3322 .kill
= service_kill
,
3324 .serialize
= service_serialize
,
3325 .deserialize_item
= service_deserialize_item
,
3327 .active_state
= service_active_state
,
3328 .sub_state_to_string
= service_sub_state_to_string
,
3330 .check_gc
= service_check_gc
,
3331 .check_snapshot
= service_check_snapshot
,
3333 .sigchld_event
= service_sigchld_event
,
3334 .timer_event
= service_timer_event
,
3336 .reset_failed
= service_reset_failed
,
3338 .cgroup_notify_empty
= service_cgroup_notify_event
,
3339 .notify_message
= service_notify_message
,
3341 .bus_name_owner_change
= service_bus_name_owner_change
,
3342 .bus_query_pid_done
= service_bus_query_pid_done
,
3344 .bus_interface
= "org.freedesktop.systemd1.Service",
3345 .bus_message_handler
= bus_service_message_handler
,
3346 .bus_invalidating_properties
= bus_service_invalidating_properties
,
3348 #ifdef HAVE_SYSV_COMPAT
3349 .enumerate
= service_enumerate