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/>.
26 #include <sys/reboot.h>
31 #include "load-fragment.h"
32 #include "load-dropin.h"
35 #include "unit-name.h"
36 #include "dbus-service.h"
38 #include "bus-errors.h"
39 #include "exit-status.h"
43 #ifdef HAVE_SYSV_COMPAT
45 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
47 typedef enum RunlevelType
{
56 const RunlevelType type
;
58 /* Standard SysV runlevels for start-up */
59 { "rc1.d", SPECIAL_RESCUE_TARGET
, RUNLEVEL_UP
},
60 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET
, RUNLEVEL_UP
},
61 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET
, RUNLEVEL_UP
},
62 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET
, RUNLEVEL_UP
},
63 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET
, RUNLEVEL_UP
},
66 /* SUSE style boot.d */
67 { "boot.d", SPECIAL_SYSINIT_TARGET
, RUNLEVEL_SYSINIT
},
70 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
71 /* Debian style rcS.d */
72 { "rcS.d", SPECIAL_SYSINIT_TARGET
, RUNLEVEL_SYSINIT
},
75 /* Standard SysV runlevels for shutdown */
76 { "rc0.d", SPECIAL_POWEROFF_TARGET
, RUNLEVEL_DOWN
},
77 { "rc6.d", SPECIAL_REBOOT_TARGET
, RUNLEVEL_DOWN
}
79 /* Note that the order here matters, as we read the
80 directories in this order, and we want to make sure that
81 sysv_start_priority is known when we first load the
82 unit. And that value we only know from S links. Hence
83 UP/SYSINIT must be read before DOWN */
86 #define RUNLEVELS_UP "12345"
87 /* #define RUNLEVELS_DOWN "06" */
88 #define RUNLEVELS_BOOT "bBsS"
91 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
92 [SERVICE_DEAD
] = UNIT_INACTIVE
,
93 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
94 [SERVICE_START
] = UNIT_ACTIVATING
,
95 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
96 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
97 [SERVICE_EXITED
] = UNIT_ACTIVE
,
98 [SERVICE_RELOAD
] = UNIT_RELOADING
,
99 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
100 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
101 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
102 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
103 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
104 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
105 [SERVICE_FAILED
] = UNIT_FAILED
,
106 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
109 static void service_init(Unit
*u
) {
110 Service
*s
= SERVICE(u
);
113 assert(u
->load_state
== UNIT_STUB
);
115 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
116 s
->restart_usec
= DEFAULT_RESTART_USEC
;
118 s
->watchdog_watch
.type
= WATCH_INVALID
;
120 s
->timer_watch
.type
= WATCH_INVALID
;
121 #ifdef HAVE_SYSV_COMPAT
122 s
->sysv_start_priority
= -1;
123 s
->sysv_start_priority_from_rcnd
= -1;
126 s
->guess_main_pid
= true;
128 exec_context_init(&s
->exec_context
);
130 RATELIMIT_INIT(s
->start_limit
, 10*USEC_PER_SEC
, 5);
132 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
135 static void service_unwatch_control_pid(Service
*s
) {
138 if (s
->control_pid
<= 0)
141 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
145 static void service_unwatch_main_pid(Service
*s
) {
148 if (s
->main_pid
<= 0)
151 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
155 static void service_unwatch_pid_file(Service
*s
) {
156 if (!s
->pid_file_pathspec
)
159 log_debug("Stopping watch for %s's PID file %s", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
160 path_spec_unwatch(s
->pid_file_pathspec
, UNIT(s
));
161 path_spec_done(s
->pid_file_pathspec
);
162 free(s
->pid_file_pathspec
);
163 s
->pid_file_pathspec
= NULL
;
166 static int service_set_main_pid(Service
*s
, pid_t pid
) {
178 s
->main_pid_known
= true;
180 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
181 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
182 UNIT(s
)->id
, (unsigned long) pid
);
184 s
->main_pid_alien
= true;
186 s
->main_pid_alien
= false;
188 exec_status_start(&s
->main_exec_status
, pid
);
193 static void service_close_socket_fd(Service
*s
) {
196 if (s
->socket_fd
< 0)
199 close_nointr_nofail(s
->socket_fd
);
203 static void service_connection_unref(Service
*s
) {
206 if (!UNIT_DEREF(s
->accept_socket
))
209 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
210 unit_ref_unset(&s
->accept_socket
);
213 static void service_stop_watchdog(Service
*s
) {
216 unit_unwatch_timer(UNIT(s
), &s
->watchdog_watch
);
217 s
->watchdog_timestamp
.realtime
= 0;
218 s
->watchdog_timestamp
.monotonic
= 0;
221 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
);
223 static void service_handle_watchdog(Service
*s
) {
229 if (s
->watchdog_usec
== 0)
232 offset
= now(CLOCK_MONOTONIC
) - s
->watchdog_timestamp
.monotonic
;
233 if (offset
>= s
->watchdog_usec
) {
234 log_error("%s watchdog timeout!", UNIT(s
)->id
);
235 service_enter_dead(s
, SERVICE_FAILURE_WATCHDOG
, true);
239 r
= unit_watch_timer(UNIT(s
), s
->watchdog_usec
- offset
, &s
->watchdog_watch
);
241 log_warning("%s failed to install watchdog timer: %s", UNIT(s
)->id
, strerror(-r
));
244 static void service_reset_watchdog(Service
*s
) {
247 dual_timestamp_get(&s
->watchdog_timestamp
);
248 service_handle_watchdog(s
);
251 static void service_done(Unit
*u
) {
252 Service
*s
= SERVICE(u
);
259 #ifdef HAVE_SYSV_COMPAT
263 free(s
->sysv_runlevels
);
264 s
->sysv_runlevels
= NULL
;
267 free(s
->status_text
);
268 s
->status_text
= NULL
;
270 exec_context_done(&s
->exec_context
);
271 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
272 s
->control_command
= NULL
;
273 s
->main_command
= NULL
;
275 /* This will leak a process, but at least no memory or any of
277 service_unwatch_main_pid(s
);
278 service_unwatch_control_pid(s
);
279 service_unwatch_pid_file(s
);
282 unit_unwatch_bus_name(u
, s
->bus_name
);
287 service_close_socket_fd(s
);
288 service_connection_unref(s
);
290 unit_ref_unset(&s
->accept_socket
);
292 service_stop_watchdog(s
);
294 unit_unwatch_timer(u
, &s
->timer_watch
);
297 #ifdef HAVE_SYSV_COMPAT
298 static char *sysv_translate_name(const char *name
) {
301 if (!(r
= new(char, strlen(name
) + sizeof(".service"))))
304 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
305 if (endswith(name
, ".sh"))
306 /* Drop Debian-style .sh suffix */
307 strcpy(stpcpy(r
, name
) - 3, ".service");
310 if (startswith(name
, "boot."))
311 /* Drop SuSE-style boot. prefix */
312 strcpy(stpcpy(r
, name
+ 5), ".service");
314 #ifdef TARGET_FRUGALWARE
315 if (startswith(name
, "rc."))
316 /* Drop Frugalware-style rc. prefix */
317 strcpy(stpcpy(r
, name
+ 3), ".service");
320 /* Normal init scripts */
321 strcpy(stpcpy(r
, name
), ".service");
326 static int sysv_translate_facility(const char *name
, const char *filename
, char **_r
) {
328 /* We silently ignore the $ prefix here. According to the LSB
329 * spec it simply indicates whether something is a
330 * standardized name or a distribution-specific one. Since we
331 * just follow what already exists and do not introduce new
332 * uses or names we don't care who introduced a new name. */
334 static const char * const table
[] = {
335 /* LSB defined facilities */
336 "local_fs", SPECIAL_LOCAL_FS_TARGET
,
337 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
339 /* Due to unfortunate name selection in Mandriva,
340 * $network is provided by network-up which is ordered
341 * after network which actually starts interfaces.
342 * To break the loop, just ignore it */
343 "network", SPECIAL_NETWORK_TARGET
,
345 "named", SPECIAL_NSS_LOOKUP_TARGET
,
346 "portmap", SPECIAL_RPCBIND_TARGET
,
347 "remote_fs", SPECIAL_REMOTE_FS_TARGET
,
348 "syslog", SPECIAL_SYSLOG_TARGET
,
349 "time", SPECIAL_TIME_SYNC_TARGET
,
351 /* common extensions */
352 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
353 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
,
356 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
357 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
361 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
362 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
363 "httpd", SPECIAL_HTTP_DAEMON_TARGET
,
367 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
378 n
= *name
== '$' ? name
+ 1 : name
;
380 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2) {
382 if (!streq(table
[i
], n
))
388 if (!(r
= strdup(table
[i
+1])))
394 /* If we don't know this name, fallback heuristics to figure
395 * out whether something is a target or a service alias. */
398 if (!unit_prefix_is_valid(n
))
401 /* Facilities starting with $ are most likely targets */
402 r
= unit_name_build(n
, NULL
, ".target");
403 } else if (filename
&& streq(name
, filename
))
404 /* Names equaling the file name of the services are redundant */
407 /* Everything else we assume to be normal service names */
408 r
= sysv_translate_name(n
);
419 static int sysv_fix_order(Service
*s
) {
425 if (s
->sysv_start_priority
< 0)
428 /* For each pair of services where at least one lacks a LSB
429 * header, we use the start priority value to order things. */
431 LIST_FOREACH(units_by_type
, other
, UNIT(s
)->manager
->units_by_type
[UNIT_SERVICE
]) {
434 bool special_s
, special_t
;
441 if (UNIT(t
)->load_state
!= UNIT_LOADED
)
444 if (t
->sysv_start_priority
< 0)
447 /* If both units have modern headers we don't care
448 * about the priorities */
449 if ((UNIT(s
)->fragment_path
|| s
->sysv_has_lsb
) &&
450 (UNIT(t
)->fragment_path
|| t
->sysv_has_lsb
))
453 special_s
= s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
);
454 special_t
= t
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, t
->sysv_runlevels
);
456 if (special_t
&& !special_s
)
458 else if (special_s
&& !special_t
)
460 else if (t
->sysv_start_priority
< s
->sysv_start_priority
)
462 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
467 /* FIXME: Maybe we should compare the name here lexicographically? */
469 if ((r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
476 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
479 if (!(c
= new0(ExecCommand
, 1)))
482 if (!(c
->path
= strdup(path
))) {
487 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
496 static int sysv_exec_commands(Service
*s
) {
500 assert(s
->sysv_path
);
502 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
504 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
506 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
508 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
510 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
512 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
517 static int service_load_sysv_path(Service
*s
, const char *path
) {
528 char *short_description
= NULL
, *long_description
= NULL
, *chkconfig_description
= NULL
, *description
;
536 if (!(f
= fopen(path
, "re"))) {
537 r
= errno
== ENOENT
? 0 : -errno
;
542 if (fstat(fileno(f
), &st
) < 0) {
548 if (!(s
->sysv_path
= strdup(path
))) {
553 s
->sysv_mtime
= timespec_load(&st
.st_mtim
);
555 if (null_or_empty(&st
)) {
556 u
->load_state
= UNIT_MASKED
;
562 char l
[LINE_MAX
], *t
;
564 if (!fgets(l
, sizeof(l
), f
)) {
569 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
579 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
581 s
->sysv_has_lsb
= true;
585 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
591 t
+= strspn(t
, WHITESPACE
);
593 if (state
== NORMAL
) {
595 /* Try to parse Red Hat style chkconfig headers */
597 if (startswith_no_case(t
, "chkconfig:")) {
599 char runlevels
[16], *k
;
603 if (sscanf(t
+10, "%15s %i %*i",
605 &start_priority
) != 2) {
607 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
611 /* A start priority gathered from the
612 * symlink farms is preferred over the
613 * data from the LSB header. */
614 if (start_priority
< 0 || start_priority
> 99)
615 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
617 s
->sysv_start_priority
= start_priority
;
619 char_array_0(runlevels
);
620 k
= delete_chars(runlevels
, WHITESPACE
"-");
625 if (!(d
= strdup(k
))) {
630 free(s
->sysv_runlevels
);
631 s
->sysv_runlevels
= d
;
634 } else if (startswith_no_case(t
, "description:")) {
636 size_t k
= strlen(t
);
640 if (t
[k
-1] == '\\') {
645 if ((j
= strstrip(t
+12)) && *j
) {
646 if (!(d
= strdup(j
))) {
653 free(chkconfig_description
);
654 chkconfig_description
= d
;
656 } else if (startswith_no_case(t
, "pidfile:")) {
663 if (!path_is_absolute(fn
)) {
664 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
668 if (!(fn
= strdup(fn
))) {
677 } else if (state
== DESCRIPTION
) {
679 /* Try to parse Red Hat style description
682 size_t k
= strlen(t
);
690 if ((j
= strstrip(t
)) && *j
) {
693 if (chkconfig_description
)
694 d
= join(chkconfig_description
, " ", j
, NULL
);
703 free(chkconfig_description
);
704 chkconfig_description
= d
;
707 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
709 if (startswith_no_case(t
, "Provides:")) {
715 FOREACH_WORD_QUOTED(w
, z
, t
+9, i
) {
718 if (!(n
= strndup(w
, z
))) {
723 r
= sysv_translate_facility(n
, file_name_from_path(path
), &m
);
732 if (unit_name_to_type(m
) == UNIT_SERVICE
)
733 r
= unit_add_name(u
, m
);
740 * indication that the
742 * now available. This
745 * targets do NOT pull
748 r
= unit_add_two_dependencies_by_name(u
, UNIT_BEFORE
, UNIT_WANTS
, m
, NULL
, true);
751 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path
, line
, m
, strerror(-r
));
756 } else if (startswith_no_case(t
, "Required-Start:") ||
757 startswith_no_case(t
, "Should-Start:") ||
758 startswith_no_case(t
, "X-Start-Before:") ||
759 startswith_no_case(t
, "X-Start-After:")) {
765 FOREACH_WORD_QUOTED(w
, z
, strchr(t
, ':')+1, i
) {
768 if (!(n
= strndup(w
, z
))) {
773 r
= sysv_translate_facility(n
, file_name_from_path(path
), &m
);
776 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path
, line
, n
, strerror(-r
));
786 r
= unit_add_dependency_by_name(u
, startswith_no_case(t
, "X-Start-Before:") ? UNIT_BEFORE
: UNIT_AFTER
, m
, NULL
, true);
789 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path
, line
, m
, strerror(-r
));
793 } else if (startswith_no_case(t
, "Default-Start:")) {
798 k
= delete_chars(t
+14, WHITESPACE
"-");
801 if (!(d
= strdup(k
))) {
806 free(s
->sysv_runlevels
);
807 s
->sysv_runlevels
= d
;
810 } else if (startswith_no_case(t
, "Description:")) {
813 state
= LSB_DESCRIPTION
;
815 if ((j
= strstrip(t
+12)) && *j
) {
816 if (!(d
= strdup(j
))) {
823 free(long_description
);
824 long_description
= d
;
826 } else if (startswith_no_case(t
, "Short-Description:")) {
831 if ((j
= strstrip(t
+18)) && *j
) {
832 if (!(d
= strdup(j
))) {
839 free(short_description
);
840 short_description
= d
;
842 } else if (state
== LSB_DESCRIPTION
) {
844 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
847 if ((j
= strstrip(t
)) && *j
) {
850 if (long_description
)
851 d
= join(long_description
, " ", t
, NULL
);
860 free(long_description
);
861 long_description
= d
;
870 if ((r
= sysv_exec_commands(s
)) < 0)
872 if (s
->sysv_runlevels
&&
873 chars_intersect(RUNLEVELS_BOOT
, s
->sysv_runlevels
) &&
874 chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
)) {
875 /* Service has both boot and "up" runlevels
876 configured. Kill the "up" ones. */
877 delete_chars(s
->sysv_runlevels
, RUNLEVELS_UP
);
880 if (s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
)) {
881 /* If there a runlevels configured for this service
882 * but none of the standard ones, then we assume this
883 * is some special kind of service (which might be
884 * needed for early boot) and don't create any links
887 UNIT(s
)->default_dependencies
= false;
889 /* Don't timeout special services during boot (like fsck) */
892 s
->timeout_usec
= DEFAULT_SYSV_TIMEOUT_USEC
;
894 /* Special setting for all SysV services */
895 s
->type
= SERVICE_FORKING
;
896 s
->remain_after_exit
= !s
->pid_file
;
897 s
->guess_main_pid
= false;
898 s
->restart
= SERVICE_RESTART_NO
;
899 s
->exec_context
.ignore_sigpipe
= false;
901 if (UNIT(s
)->manager
->sysv_console
)
902 s
->exec_context
.std_output
= EXEC_OUTPUT_JOURNAL_AND_CONSOLE
;
904 s
->exec_context
.kill_mode
= KILL_PROCESS
;
906 /* We use the long description only if
907 * no short description is set. */
909 if (short_description
)
910 description
= short_description
;
911 else if (chkconfig_description
)
912 description
= chkconfig_description
;
913 else if (long_description
)
914 description
= long_description
;
921 if (!(d
= strappend(s
->sysv_has_lsb
? "LSB: " : "SYSV: ", description
))) {
929 /* The priority that has been set in /etc/rcN.d/ hierarchies
930 * takes precedence over what is stored as default in the LSB
932 if (s
->sysv_start_priority_from_rcnd
>= 0)
933 s
->sysv_start_priority
= s
->sysv_start_priority_from_rcnd
;
935 u
->load_state
= UNIT_LOADED
;
943 free(short_description
);
944 free(long_description
);
945 free(chkconfig_description
);
950 static int service_load_sysv_name(Service
*s
, const char *name
) {
956 /* For SysV services we strip the boot.*, rc.* and *.sh
957 * prefixes/suffixes. */
958 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
959 if (endswith(name
, ".sh.service"))
964 if (startswith(name
, "boot."))
968 #ifdef TARGET_FRUGALWARE
969 if (startswith(name
, "rc."))
973 STRV_FOREACH(p
, UNIT(s
)->manager
->lookup_paths
.sysvinit_path
) {
977 path
= join(*p
, "/", name
, NULL
);
981 assert(endswith(path
, ".service"));
982 path
[strlen(path
)-8] = 0;
984 r
= service_load_sysv_path(s
, path
);
986 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
987 if (r
>= 0 && UNIT(s
)->load_state
== UNIT_STUB
) {
988 /* Try Debian style *.sh source'able init scripts */
990 r
= service_load_sysv_path(s
, path
);
996 if (r
>= 0 && UNIT(s
)->load_state
== UNIT_STUB
) {
997 /* Try SUSE style boot.* init scripts */
999 path
= join(*p
, "/boot.", name
, NULL
);
1003 /* Drop .service suffix */
1004 path
[strlen(path
)-8] = 0;
1005 r
= service_load_sysv_path(s
, path
);
1010 #ifdef TARGET_FRUGALWARE
1011 if (r
>= 0 && UNIT(s
)->load_state
== UNIT_STUB
) {
1012 /* Try Frugalware style rc.* init scripts */
1014 path
= join(*p
, "/rc.", name
, NULL
);
1018 /* Drop .service suffix */
1019 path
[strlen(path
)-8] = 0;
1020 r
= service_load_sysv_path(s
, path
);
1028 if ((UNIT(s
)->load_state
!= UNIT_STUB
))
1035 static int service_load_sysv(Service
*s
) {
1042 /* Load service data from SysV init scripts, preferably with
1043 * LSB headers ... */
1045 if (strv_isempty(UNIT(s
)->manager
->lookup_paths
.sysvinit_path
))
1048 if ((t
= UNIT(s
)->id
))
1049 if ((r
= service_load_sysv_name(s
, t
)) < 0)
1052 if (UNIT(s
)->load_state
== UNIT_STUB
)
1053 SET_FOREACH(t
, UNIT(s
)->names
, i
) {
1054 if (t
== UNIT(s
)->id
)
1057 if ((r
= service_load_sysv_name(s
, t
)) < 0)
1060 if (UNIT(s
)->load_state
!= UNIT_STUB
)
1068 static int fsck_fix_order(Service
*s
) {
1074 if (s
->fsck_passno
<= 0)
1077 /* For each pair of services where both have an fsck priority
1078 * we order things based on it. */
1080 LIST_FOREACH(units_by_type
, other
, UNIT(s
)->manager
->units_by_type
[UNIT_SERVICE
]) {
1089 if (UNIT(t
)->load_state
!= UNIT_LOADED
)
1092 if (t
->fsck_passno
<= 0)
1095 if (t
->fsck_passno
< s
->fsck_passno
)
1097 else if (t
->fsck_passno
> s
->fsck_passno
)
1102 if ((r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
1109 static int service_verify(Service
*s
) {
1112 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
1115 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
1116 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s
)->id
);
1120 if (s
->type
!= SERVICE_ONESHOT
&&
1121 s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
1122 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
1126 if (s
->type
== SERVICE_ONESHOT
&&
1127 s
->exec_command
[SERVICE_EXEC_RELOAD
]) {
1128 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
1132 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
1133 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s
)->id
);
1137 if (s
->exec_context
.pam_name
&& s
->exec_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
1138 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s
)->id
);
1145 static int service_add_default_dependencies(Service
*s
) {
1150 /* Add a number of automatic dependencies useful for the
1151 * majority of services. */
1153 /* First, pull in base system */
1154 if (UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
) {
1156 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
1159 } else if (UNIT(s
)->manager
->running_as
== MANAGER_USER
) {
1161 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SOCKETS_TARGET
, NULL
, true)) < 0)
1165 /* Second, activate normal shutdown */
1166 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
1169 static void service_fix_output(Service
*s
) {
1172 /* If nothing has been explicitly configured, patch default
1173 * output in. If input is socket/tty we avoid this however,
1174 * since in that case we want output to default to the same
1175 * place as we read input from. */
1177 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
1178 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
1179 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
1180 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
1182 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
1183 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
1184 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
1187 static int service_load(Unit
*u
) {
1189 Service
*s
= SERVICE(u
);
1193 /* Load a .service file */
1194 if ((r
= unit_load_fragment(u
)) < 0)
1197 #ifdef HAVE_SYSV_COMPAT
1198 /* Load a classic init script as a fallback, if we couldn't find anything */
1199 if (u
->load_state
== UNIT_STUB
)
1200 if ((r
= service_load_sysv(s
)) < 0)
1204 /* Still nothing found? Then let's give up */
1205 if (u
->load_state
== UNIT_STUB
)
1208 /* We were able to load something, then let's add in the
1209 * dropin directories. */
1210 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
1213 /* This is a new unit? Then let's add in some extras */
1214 if (u
->load_state
== UNIT_LOADED
) {
1215 service_fix_output(s
);
1217 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
1220 if ((r
= unit_add_default_cgroups(u
)) < 0)
1223 #ifdef HAVE_SYSV_COMPAT
1224 if ((r
= sysv_fix_order(s
)) < 0)
1228 if ((r
= fsck_fix_order(s
)) < 0)
1232 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
1235 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
1236 s
->notify_access
= NOTIFY_MAIN
;
1238 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
1239 s
->notify_access
= NOTIFY_MAIN
;
1241 if (s
->type
== SERVICE_DBUS
|| s
->bus_name
)
1242 if ((r
= unit_add_two_dependencies_by_name(u
, UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, NULL
, true)) < 0)
1245 if (UNIT(s
)->default_dependencies
)
1246 if ((r
= service_add_default_dependencies(s
)) < 0)
1250 return service_verify(s
);
1253 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
1255 ServiceExecCommand c
;
1256 Service
*s
= SERVICE(u
);
1257 const char *prefix2
;
1262 p2
= strappend(prefix
, "\t");
1263 prefix2
= p2
? p2
: prefix
;
1266 "%sService State: %s\n"
1268 "%sReload Result: %s\n"
1269 "%sPermissionsStartOnly: %s\n"
1270 "%sRootDirectoryStartOnly: %s\n"
1271 "%sRemainAfterExit: %s\n"
1272 "%sGuessMainPID: %s\n"
1275 "%sNotifyAccess: %s\n",
1276 prefix
, service_state_to_string(s
->state
),
1277 prefix
, service_result_to_string(s
->result
),
1278 prefix
, service_result_to_string(s
->reload_result
),
1279 prefix
, yes_no(s
->permissions_start_only
),
1280 prefix
, yes_no(s
->root_directory_start_only
),
1281 prefix
, yes_no(s
->remain_after_exit
),
1282 prefix
, yes_no(s
->guess_main_pid
),
1283 prefix
, service_type_to_string(s
->type
),
1284 prefix
, service_restart_to_string(s
->restart
),
1285 prefix
, notify_access_to_string(s
->notify_access
));
1287 if (s
->control_pid
> 0)
1289 "%sControl PID: %lu\n",
1290 prefix
, (unsigned long) s
->control_pid
);
1292 if (s
->main_pid
> 0)
1295 "%sMain PID Known: %s\n"
1296 "%sMain PID Alien: %s\n",
1297 prefix
, (unsigned long) s
->main_pid
,
1298 prefix
, yes_no(s
->main_pid_known
),
1299 prefix
, yes_no(s
->main_pid_alien
));
1304 prefix
, s
->pid_file
);
1309 "%sBus Name Good: %s\n",
1310 prefix
, s
->bus_name
,
1311 prefix
, yes_no(s
->bus_name_good
));
1313 exec_context_dump(&s
->exec_context
, f
, prefix
);
1315 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
1317 if (!s
->exec_command
[c
])
1320 fprintf(f
, "%s-> %s:\n",
1321 prefix
, service_exec_command_to_string(c
));
1323 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
1326 #ifdef HAVE_SYSV_COMPAT
1329 "%sSysV Init Script Path: %s\n"
1330 "%sSysV Init Script has LSB Header: %s\n"
1331 "%sSysVEnabled: %s\n",
1332 prefix
, s
->sysv_path
,
1333 prefix
, yes_no(s
->sysv_has_lsb
),
1334 prefix
, yes_no(s
->sysv_enabled
));
1336 if (s
->sysv_start_priority
>= 0)
1338 "%sSysVStartPriority: %i\n",
1339 prefix
, s
->sysv_start_priority
);
1341 if (s
->sysv_runlevels
)
1342 fprintf(f
, "%sSysVRunLevels: %s\n",
1343 prefix
, s
->sysv_runlevels
);
1346 if (s
->fsck_passno
> 0)
1348 "%sFsckPassNo: %i\n",
1349 prefix
, s
->fsck_passno
);
1352 fprintf(f
, "%sStatus Text: %s\n",
1353 prefix
, s
->status_text
);
1358 static int service_load_pid_file(Service
*s
, bool may_warn
) {
1368 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0) {
1370 log_info("PID file %s not readable (yet?) after %s.",
1371 s
->pid_file
, service_state_to_string(s
->state
));
1375 r
= parse_pid(k
, &pid
);
1381 if (kill(pid
, 0) < 0 && errno
!= EPERM
) {
1383 log_info("PID %lu read from file %s does not exist.",
1384 (unsigned long) pid
, s
->pid_file
);
1388 if (s
->main_pid_known
) {
1389 if (pid
== s
->main_pid
)
1392 log_debug("Main PID changing: %lu -> %lu",
1393 (unsigned long) s
->main_pid
, (unsigned long) pid
);
1394 service_unwatch_main_pid(s
);
1395 s
->main_pid_known
= false;
1397 log_debug("Main PID loaded: %lu", (unsigned long) pid
);
1399 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1402 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1403 /* FIXME: we need to do something here */
1409 static int service_search_main_pid(Service
*s
) {
1415 /* If we know it anyway, don't ever fallback to unreliable
1417 if (s
->main_pid_known
)
1420 if (!s
->guess_main_pid
)
1423 assert(s
->main_pid
<= 0);
1425 if ((pid
= cgroup_bonding_search_main_pid_list(UNIT(s
)->cgroup_bondings
)) <= 0)
1428 log_debug("Main PID guessed: %lu", (unsigned long) pid
);
1429 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1432 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1433 /* FIXME: we need to do something here */
1439 static void service_notify_sockets_dead(Service
*s
) {
1445 /* Notifies all our sockets when we die */
1447 if (s
->socket_fd
>= 0)
1450 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
)
1451 if (u
->type
== UNIT_SOCKET
)
1452 socket_notify_service_dead(SOCKET(u
));
1457 static void service_set_state(Service
*s
, ServiceState state
) {
1458 ServiceState old_state
;
1461 old_state
= s
->state
;
1464 service_unwatch_pid_file(s
);
1466 if (state
!= SERVICE_START_PRE
&&
1467 state
!= SERVICE_START
&&
1468 state
!= SERVICE_START_POST
&&
1469 state
!= SERVICE_RELOAD
&&
1470 state
!= SERVICE_STOP
&&
1471 state
!= SERVICE_STOP_SIGTERM
&&
1472 state
!= SERVICE_STOP_SIGKILL
&&
1473 state
!= SERVICE_STOP_POST
&&
1474 state
!= SERVICE_FINAL_SIGTERM
&&
1475 state
!= SERVICE_FINAL_SIGKILL
&&
1476 state
!= SERVICE_AUTO_RESTART
)
1477 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1479 if (state
!= SERVICE_START
&&
1480 state
!= SERVICE_START_POST
&&
1481 state
!= SERVICE_RUNNING
&&
1482 state
!= SERVICE_RELOAD
&&
1483 state
!= SERVICE_STOP
&&
1484 state
!= SERVICE_STOP_SIGTERM
&&
1485 state
!= SERVICE_STOP_SIGKILL
) {
1486 service_unwatch_main_pid(s
);
1487 s
->main_command
= NULL
;
1490 if (state
!= SERVICE_START_PRE
&&
1491 state
!= SERVICE_START
&&
1492 state
!= SERVICE_START_POST
&&
1493 state
!= SERVICE_RELOAD
&&
1494 state
!= SERVICE_STOP
&&
1495 state
!= SERVICE_STOP_SIGTERM
&&
1496 state
!= SERVICE_STOP_SIGKILL
&&
1497 state
!= SERVICE_STOP_POST
&&
1498 state
!= SERVICE_FINAL_SIGTERM
&&
1499 state
!= SERVICE_FINAL_SIGKILL
) {
1500 service_unwatch_control_pid(s
);
1501 s
->control_command
= NULL
;
1502 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1505 if (state
== SERVICE_DEAD
||
1506 state
== SERVICE_STOP
||
1507 state
== SERVICE_STOP_SIGTERM
||
1508 state
== SERVICE_STOP_SIGKILL
||
1509 state
== SERVICE_STOP_POST
||
1510 state
== SERVICE_FINAL_SIGTERM
||
1511 state
== SERVICE_FINAL_SIGKILL
||
1512 state
== SERVICE_FAILED
||
1513 state
== SERVICE_AUTO_RESTART
)
1514 service_notify_sockets_dead(s
);
1516 if (state
!= SERVICE_START_PRE
&&
1517 state
!= SERVICE_START
&&
1518 state
!= SERVICE_START_POST
&&
1519 state
!= SERVICE_RUNNING
&&
1520 state
!= SERVICE_RELOAD
&&
1521 state
!= SERVICE_STOP
&&
1522 state
!= SERVICE_STOP_SIGTERM
&&
1523 state
!= SERVICE_STOP_SIGKILL
&&
1524 state
!= SERVICE_STOP_POST
&&
1525 state
!= SERVICE_FINAL_SIGTERM
&&
1526 state
!= SERVICE_FINAL_SIGKILL
&&
1527 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
1528 service_close_socket_fd(s
);
1529 service_connection_unref(s
);
1532 if (state
== SERVICE_STOP
)
1533 service_stop_watchdog(s
);
1535 /* For the inactive states unit_notify() will trim the cgroup,
1536 * but for exit we have to do that ourselves... */
1537 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
1538 cgroup_bonding_trim_list(UNIT(s
)->cgroup_bondings
, true);
1540 if (old_state
!= state
)
1541 log_debug("%s changed %s -> %s", UNIT(s
)->id
, service_state_to_string(old_state
), service_state_to_string(state
));
1543 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
1544 s
->reload_result
= SERVICE_SUCCESS
;
1547 static int service_coldplug(Unit
*u
) {
1548 Service
*s
= SERVICE(u
);
1552 assert(s
->state
== SERVICE_DEAD
);
1554 if (s
->deserialized_state
!= s
->state
) {
1556 if (s
->deserialized_state
== SERVICE_START_PRE
||
1557 s
->deserialized_state
== SERVICE_START
||
1558 s
->deserialized_state
== SERVICE_START_POST
||
1559 s
->deserialized_state
== SERVICE_RELOAD
||
1560 s
->deserialized_state
== SERVICE_STOP
||
1561 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1562 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1563 s
->deserialized_state
== SERVICE_STOP_POST
||
1564 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1565 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
||
1566 s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
1568 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
|| s
->timeout_usec
> 0) {
1571 k
= s
->deserialized_state
== SERVICE_AUTO_RESTART
? s
->restart_usec
: s
->timeout_usec
;
1573 if ((r
= unit_watch_timer(UNIT(s
), k
, &s
->timer_watch
)) < 0)
1578 if ((s
->deserialized_state
== SERVICE_START
&&
1579 (s
->type
== SERVICE_FORKING
||
1580 s
->type
== SERVICE_DBUS
||
1581 s
->type
== SERVICE_ONESHOT
||
1582 s
->type
== SERVICE_NOTIFY
)) ||
1583 s
->deserialized_state
== SERVICE_START_POST
||
1584 s
->deserialized_state
== SERVICE_RUNNING
||
1585 s
->deserialized_state
== SERVICE_RELOAD
||
1586 s
->deserialized_state
== SERVICE_STOP
||
1587 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1588 s
->deserialized_state
== SERVICE_STOP_SIGKILL
)
1589 if (s
->main_pid
> 0)
1590 if ((r
= unit_watch_pid(UNIT(s
), s
->main_pid
)) < 0)
1593 if (s
->deserialized_state
== SERVICE_START_PRE
||
1594 s
->deserialized_state
== SERVICE_START
||
1595 s
->deserialized_state
== SERVICE_START_POST
||
1596 s
->deserialized_state
== SERVICE_RELOAD
||
1597 s
->deserialized_state
== SERVICE_STOP
||
1598 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1599 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1600 s
->deserialized_state
== SERVICE_STOP_POST
||
1601 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1602 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
)
1603 if (s
->control_pid
> 0)
1604 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1607 if (s
->deserialized_state
== SERVICE_START_POST
||
1608 s
->deserialized_state
== SERVICE_RUNNING
)
1609 service_handle_watchdog(s
);
1611 service_set_state(s
, s
->deserialized_state
);
1616 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1620 unsigned rn_fds
= 0;
1627 if (s
->socket_fd
>= 0)
1630 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1635 if (u
->type
!= UNIT_SOCKET
)
1640 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1652 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1658 memcpy(t
, rfds
, rn_fds
* sizeof(int));
1659 memcpy(t
+rn_fds
, cfds
, cn_fds
* sizeof(int));
1664 rn_fds
= rn_fds
+cn_fds
;
1679 static int service_spawn(
1684 bool apply_permissions
,
1686 bool apply_tty_stdin
,
1687 bool set_notify_socket
,
1692 int *fds
= NULL
, *fdsbuf
= NULL
;
1693 unsigned n_fds
= 0, n_env
= 0;
1694 char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
;
1701 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1702 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1703 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1705 if (s
->socket_fd
>= 0) {
1706 fds
= &s
->socket_fd
;
1709 if ((r
= service_collect_fds(s
, &fdsbuf
, &n_fds
)) < 0)
1716 if (timeout
&& s
->timeout_usec
) {
1717 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1720 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1722 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1727 if (!(our_env
= new0(char*, 4))) {
1732 if (set_notify_socket
)
1733 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1738 if (s
->main_pid
> 0)
1739 if (asprintf(our_env
+ n_env
++, "MAINPID=%lu", (unsigned long) s
->main_pid
) < 0) {
1744 if (s
->watchdog_usec
> 0)
1745 if (asprintf(our_env
+ n_env
++, "WATCHDOG_USEC=%llu", (unsigned long long) s
->watchdog_usec
) < 0) {
1750 if (!(final_env
= strv_env_merge(2,
1751 UNIT(s
)->manager
->environment
,
1766 UNIT(s
)->manager
->confirm_spawn
,
1767 UNIT(s
)->cgroup_bondings
,
1768 UNIT(s
)->cgroup_attributes
,
1775 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1776 /* FIXME: we need to do something here */
1782 strv_free(final_env
);
1792 strv_free(final_env
);
1795 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1800 static int main_pid_good(Service
*s
) {
1803 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1806 /* If we know the pid file, then lets just check if it is
1808 if (s
->main_pid_known
) {
1810 /* If it's an alien child let's check if it is still
1812 if (s
->main_pid_alien
)
1813 return kill(s
->main_pid
, 0) >= 0 || errno
!= ESRCH
;
1815 /* .. otherwise assume we'll get a SIGCHLD for it,
1816 * which we really should wait for to collect exit
1817 * status and code */
1818 return s
->main_pid
> 0;
1821 /* We don't know the pid */
1825 static int control_pid_good(Service
*s
) {
1828 return s
->control_pid
> 0;
1831 static int cgroup_good(Service
*s
) {
1836 if ((r
= cgroup_bonding_is_empty_list(UNIT(s
)->cgroup_bondings
)) < 0)
1842 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1846 if (f
!= SERVICE_SUCCESS
)
1849 if (allow_restart
&&
1850 !s
->forbid_restart
&&
1851 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1852 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& s
->result
== SERVICE_SUCCESS
) ||
1853 (s
->restart
== SERVICE_RESTART_ON_FAILURE
&& s
->result
!= SERVICE_SUCCESS
) ||
1854 (s
->restart
== SERVICE_RESTART_ON_ABORT
&& (s
->result
== SERVICE_FAILURE_SIGNAL
||
1855 s
->result
== SERVICE_FAILURE_CORE_DUMP
)))) {
1857 r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
);
1861 service_set_state(s
, SERVICE_AUTO_RESTART
);
1863 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1865 s
->forbid_restart
= false;
1870 log_warning("%s failed to run install restart timer: %s", UNIT(s
)->id
, strerror(-r
));
1871 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1874 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
1876 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1880 if (f
!= SERVICE_SUCCESS
)
1883 service_unwatch_control_pid(s
);
1885 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1886 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1888 if ((r
= service_spawn(s
,
1892 !s
->permissions_start_only
,
1893 !s
->root_directory_start_only
,
1896 &s
->control_pid
)) < 0)
1900 service_set_state(s
, SERVICE_STOP_POST
);
1902 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1907 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s
)->id
, strerror(-r
));
1908 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1911 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1913 Set
*pid_set
= NULL
;
1914 bool wait_for_exit
= false;
1918 if (f
!= SERVICE_SUCCESS
)
1921 if (s
->exec_context
.kill_mode
!= KILL_NONE
) {
1922 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? s
->exec_context
.kill_signal
: SIGKILL
;
1924 if (s
->main_pid
> 0) {
1925 if (kill_and_sigcont(s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1926 log_warning("Failed to kill main process %li: %m", (long) s
->main_pid
);
1928 wait_for_exit
= !s
->main_pid_alien
;
1931 if (s
->control_pid
> 0) {
1932 if (kill_and_sigcont(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1933 log_warning("Failed to kill control process %li: %m", (long) s
->control_pid
);
1935 wait_for_exit
= true;
1938 if (s
->exec_context
.kill_mode
== KILL_CONTROL_GROUP
) {
1940 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
))) {
1945 /* Exclude the main/control pids from being killed via the cgroup */
1946 if (s
->main_pid
> 0)
1947 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->main_pid
))) < 0)
1950 if (s
->control_pid
> 0)
1951 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0)
1954 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, sig
, true, pid_set
)) < 0) {
1955 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
1956 log_warning("Failed to kill control group: %s", strerror(-r
));
1958 wait_for_exit
= true;
1965 if (wait_for_exit
) {
1966 if (s
->timeout_usec
> 0)
1967 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1970 service_set_state(s
, state
);
1971 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1972 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1974 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1979 log_warning("%s failed to kill processes: %s", UNIT(s
)->id
, strerror(-r
));
1981 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1982 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1984 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1990 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1995 if (f
!= SERVICE_SUCCESS
)
1998 service_unwatch_control_pid(s
);
2000 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
2001 s
->control_command_id
= SERVICE_EXEC_STOP
;
2003 if ((r
= service_spawn(s
,
2007 !s
->permissions_start_only
,
2008 !s
->root_directory_start_only
,
2011 &s
->control_pid
)) < 0)
2014 service_set_state(s
, SERVICE_STOP
);
2016 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2021 log_warning("%s failed to run 'stop' task: %s", UNIT(s
)->id
, strerror(-r
));
2022 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2025 static void service_enter_running(Service
*s
, ServiceResult f
) {
2026 int main_pid_ok
, cgroup_ok
;
2029 if (f
!= SERVICE_SUCCESS
)
2032 main_pid_ok
= main_pid_good(s
);
2033 cgroup_ok
= cgroup_good(s
);
2035 if ((main_pid_ok
> 0 || (main_pid_ok
< 0 && cgroup_ok
!= 0)) &&
2036 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
2037 service_set_state(s
, SERVICE_RUNNING
);
2038 else if (s
->remain_after_exit
)
2039 service_set_state(s
, SERVICE_EXITED
);
2041 service_enter_stop(s
, SERVICE_SUCCESS
);
2044 static void service_enter_start_post(Service
*s
) {
2048 service_unwatch_control_pid(s
);
2050 if (s
->watchdog_usec
> 0)
2051 service_reset_watchdog(s
);
2053 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
2054 s
->control_command_id
= SERVICE_EXEC_START_POST
;
2056 if ((r
= service_spawn(s
,
2060 !s
->permissions_start_only
,
2061 !s
->root_directory_start_only
,
2064 &s
->control_pid
)) < 0)
2067 service_set_state(s
, SERVICE_START_POST
);
2069 service_enter_running(s
, SERVICE_SUCCESS
);
2074 log_warning("%s failed to run 'start-post' task: %s", UNIT(s
)->id
, strerror(-r
));
2075 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2078 static void service_enter_start(Service
*s
) {
2085 assert(s
->exec_command
[SERVICE_EXEC_START
]);
2086 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
|| s
->type
== SERVICE_ONESHOT
);
2088 if (s
->type
== SERVICE_FORKING
)
2089 service_unwatch_control_pid(s
);
2091 service_unwatch_main_pid(s
);
2093 /* We want to ensure that nobody leaks processes from
2094 * START_PRE here, so let's go on a killing spree, People
2095 * should not spawn long running processes from START_PRE. */
2096 cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, SIGKILL
, true, NULL
);
2098 if (s
->type
== SERVICE_FORKING
) {
2099 s
->control_command_id
= SERVICE_EXEC_START
;
2100 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
2102 s
->main_command
= NULL
;
2104 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2105 s
->control_command
= NULL
;
2107 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
2110 if ((r
= service_spawn(s
,
2112 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
,
2117 s
->notify_access
!= NOTIFY_NONE
,
2121 if (s
->type
== SERVICE_SIMPLE
) {
2122 /* For simple services we immediately start
2123 * the START_POST binaries. */
2125 service_set_main_pid(s
, pid
);
2126 service_enter_start_post(s
);
2128 } else if (s
->type
== SERVICE_FORKING
) {
2130 /* For forking services we wait until the start
2131 * process exited. */
2133 s
->control_pid
= pid
;
2134 service_set_state(s
, SERVICE_START
);
2136 } else if (s
->type
== SERVICE_ONESHOT
||
2137 s
->type
== SERVICE_DBUS
||
2138 s
->type
== SERVICE_NOTIFY
) {
2140 /* For oneshot services we wait until the start
2141 * process exited, too, but it is our main process. */
2143 /* For D-Bus services we know the main pid right away,
2144 * but wait for the bus name to appear on the
2145 * bus. Notify services are similar. */
2147 service_set_main_pid(s
, pid
);
2148 service_set_state(s
, SERVICE_START
);
2150 assert_not_reached("Unknown service type");
2155 log_warning("%s failed to run 'start' task: %s", UNIT(s
)->id
, strerror(-r
));
2156 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2159 static void service_enter_start_pre(Service
*s
) {
2164 service_unwatch_control_pid(s
);
2166 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
2168 /* Before we start anything, let's clear up what might
2169 * be left from previous runs. */
2170 cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, SIGKILL
, true, NULL
);
2172 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
2174 if ((r
= service_spawn(s
,
2178 !s
->permissions_start_only
,
2179 !s
->root_directory_start_only
,
2182 &s
->control_pid
)) < 0)
2185 service_set_state(s
, SERVICE_START_PRE
);
2187 service_enter_start(s
);
2192 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s
)->id
, strerror(-r
));
2193 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2196 static void service_enter_restart(Service
*s
) {
2201 dbus_error_init(&error
);
2204 log_info("Job pending for unit, delaying automatic restart.");
2206 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
2210 service_enter_dead(s
, SERVICE_SUCCESS
, false);
2212 if ((r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
)) < 0)
2215 log_debug("%s scheduled restart job.", UNIT(s
)->id
);
2219 log_warning("%s failed to schedule restart job: %s", UNIT(s
)->id
, bus_error(&error
, -r
));
2220 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
2222 dbus_error_free(&error
);
2225 static void service_enter_reload(Service
*s
) {
2230 service_unwatch_control_pid(s
);
2232 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
2233 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
2235 if ((r
= service_spawn(s
,
2239 !s
->permissions_start_only
,
2240 !s
->root_directory_start_only
,
2243 &s
->control_pid
)) < 0)
2246 service_set_state(s
, SERVICE_RELOAD
);
2248 service_enter_running(s
, SERVICE_SUCCESS
);
2253 log_warning("%s failed to run 'reload' task: %s", UNIT(s
)->id
, strerror(-r
));
2254 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2255 service_enter_running(s
, SERVICE_SUCCESS
);
2258 static void service_run_next_control(Service
*s
) {
2262 assert(s
->control_command
);
2263 assert(s
->control_command
->command_next
);
2265 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
2267 s
->control_command
= s
->control_command
->command_next
;
2268 service_unwatch_control_pid(s
);
2270 if ((r
= service_spawn(s
,
2274 !s
->permissions_start_only
,
2275 !s
->root_directory_start_only
,
2276 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
2277 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
2279 &s
->control_pid
)) < 0)
2285 log_warning("%s failed to run next control task: %s", UNIT(s
)->id
, strerror(-r
));
2287 if (s
->state
== SERVICE_START_PRE
)
2288 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2289 else if (s
->state
== SERVICE_STOP
)
2290 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2291 else if (s
->state
== SERVICE_STOP_POST
)
2292 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2293 else if (s
->state
== SERVICE_RELOAD
) {
2294 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2295 service_enter_running(s
, SERVICE_SUCCESS
);
2297 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2300 static void service_run_next_main(Service
*s
) {
2305 assert(s
->main_command
);
2306 assert(s
->main_command
->command_next
);
2307 assert(s
->type
== SERVICE_ONESHOT
);
2309 s
->main_command
= s
->main_command
->command_next
;
2310 service_unwatch_main_pid(s
);
2312 if ((r
= service_spawn(s
,
2319 s
->notify_access
!= NOTIFY_NONE
,
2323 service_set_main_pid(s
, pid
);
2328 log_warning("%s failed to run next main task: %s", UNIT(s
)->id
, strerror(-r
));
2329 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2332 static int service_start_limit_test(Service
*s
) {
2335 if (ratelimit_test(&s
->start_limit
))
2338 switch (s
->start_limit_action
) {
2340 case SERVICE_START_LIMIT_NONE
:
2341 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s
)->id
);
2344 case SERVICE_START_LIMIT_REBOOT
: {
2348 dbus_error_init(&error
);
2350 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s
)->id
);
2352 r
= manager_add_job_by_name(UNIT(s
)->manager
, JOB_START
, SPECIAL_REBOOT_TARGET
, JOB_REPLACE
, true, &error
, NULL
);
2354 log_error("Failed to reboot: %s.", bus_error(&error
, r
));
2355 dbus_error_free(&error
);
2361 case SERVICE_START_LIMIT_REBOOT_FORCE
:
2362 log_warning("%s start request repeated too quickly, force rebooting.", UNIT(s
)->id
);
2363 UNIT(s
)->manager
->exit_code
= MANAGER_REBOOT
;
2366 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE
:
2367 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s
)->id
);
2368 reboot(RB_AUTOBOOT
);
2372 log_error("start limit action=%i", s
->start_limit_action
);
2373 assert_not_reached("Unknown StartLimitAction.");
2379 static int service_start(Unit
*u
) {
2380 Service
*s
= SERVICE(u
);
2385 /* We cannot fulfill this request right now, try again later
2387 if (s
->state
== SERVICE_STOP
||
2388 s
->state
== SERVICE_STOP_SIGTERM
||
2389 s
->state
== SERVICE_STOP_SIGKILL
||
2390 s
->state
== SERVICE_STOP_POST
||
2391 s
->state
== SERVICE_FINAL_SIGTERM
||
2392 s
->state
== SERVICE_FINAL_SIGKILL
)
2395 /* Already on it! */
2396 if (s
->state
== SERVICE_START_PRE
||
2397 s
->state
== SERVICE_START
||
2398 s
->state
== SERVICE_START_POST
)
2401 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_FAILED
|| s
->state
== SERVICE_AUTO_RESTART
);
2403 /* Make sure we don't enter a busy loop of some kind. */
2404 r
= service_start_limit_test(s
);
2408 s
->result
= SERVICE_SUCCESS
;
2409 s
->reload_result
= SERVICE_SUCCESS
;
2410 s
->main_pid_known
= false;
2411 s
->main_pid_alien
= false;
2412 s
->forbid_restart
= false;
2414 service_enter_start_pre(s
);
2418 static int service_stop(Unit
*u
) {
2419 Service
*s
= SERVICE(u
);
2423 /* This is a user request, so don't do restarts on this
2425 s
->forbid_restart
= true;
2428 if (s
->state
== SERVICE_STOP
||
2429 s
->state
== SERVICE_STOP_SIGTERM
||
2430 s
->state
== SERVICE_STOP_SIGKILL
||
2431 s
->state
== SERVICE_STOP_POST
||
2432 s
->state
== SERVICE_FINAL_SIGTERM
||
2433 s
->state
== SERVICE_FINAL_SIGKILL
)
2436 /* Don't allow a restart */
2437 if (s
->state
== SERVICE_AUTO_RESTART
) {
2438 service_set_state(s
, SERVICE_DEAD
);
2442 /* If there's already something running we go directly into
2444 if (s
->state
== SERVICE_START_PRE
||
2445 s
->state
== SERVICE_START
||
2446 s
->state
== SERVICE_START_POST
||
2447 s
->state
== SERVICE_RELOAD
) {
2448 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2452 assert(s
->state
== SERVICE_RUNNING
||
2453 s
->state
== SERVICE_EXITED
);
2455 service_enter_stop(s
, SERVICE_SUCCESS
);
2459 static int service_reload(Unit
*u
) {
2460 Service
*s
= SERVICE(u
);
2464 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2466 service_enter_reload(s
);
2470 static bool service_can_reload(Unit
*u
) {
2471 Service
*s
= SERVICE(u
);
2475 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2478 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2479 Service
*s
= SERVICE(u
);
2485 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2486 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2487 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2489 if (s
->control_pid
> 0)
2490 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
2492 if (s
->main_pid_known
&& s
->main_pid
> 0)
2493 unit_serialize_item_format(u
, f
, "main-pid", "%lu", (unsigned long) s
->main_pid
);
2495 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2498 unit_serialize_item(u
, f
, "status-text", s
->status_text
);
2500 /* FIXME: There's a minor uncleanliness here: if there are
2501 * multiple commands attached here, we will start from the
2502 * first one again */
2503 if (s
->control_command_id
>= 0)
2504 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2506 if (s
->socket_fd
>= 0) {
2509 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
2512 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
2515 if (s
->main_exec_status
.pid
> 0) {
2516 unit_serialize_item_format(u
, f
, "main-exec-status-pid", "%lu", (unsigned long) s
->main_exec_status
.pid
);
2517 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2518 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2520 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2521 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2522 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2525 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
2526 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2531 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2532 Service
*s
= SERVICE(u
);
2539 if (streq(key
, "state")) {
2542 if ((state
= service_state_from_string(value
)) < 0)
2543 log_debug("Failed to parse state value %s", value
);
2545 s
->deserialized_state
= state
;
2546 } else if (streq(key
, "result")) {
2549 f
= service_result_from_string(value
);
2551 log_debug("Failed to parse result value %s", value
);
2552 else if (f
!= SERVICE_SUCCESS
)
2555 } else if (streq(key
, "reload-result")) {
2558 f
= service_result_from_string(value
);
2560 log_debug("Failed to parse reload result value %s", value
);
2561 else if (f
!= SERVICE_SUCCESS
)
2562 s
->reload_result
= f
;
2564 } else if (streq(key
, "control-pid")) {
2567 if (parse_pid(value
, &pid
) < 0)
2568 log_debug("Failed to parse control-pid value %s", value
);
2570 s
->control_pid
= pid
;
2571 } else if (streq(key
, "main-pid")) {
2574 if (parse_pid(value
, &pid
) < 0)
2575 log_debug("Failed to parse main-pid value %s", value
);
2577 service_set_main_pid(s
, (pid_t
) pid
);
2578 } else if (streq(key
, "main-pid-known")) {
2581 if ((b
= parse_boolean(value
)) < 0)
2582 log_debug("Failed to parse main-pid-known value %s", value
);
2584 s
->main_pid_known
= b
;
2585 } else if (streq(key
, "status-text")) {
2588 if ((t
= strdup(value
))) {
2589 free(s
->status_text
);
2593 } else if (streq(key
, "control-command")) {
2594 ServiceExecCommand id
;
2596 if ((id
= service_exec_command_from_string(value
)) < 0)
2597 log_debug("Failed to parse exec-command value %s", value
);
2599 s
->control_command_id
= id
;
2600 s
->control_command
= s
->exec_command
[id
];
2602 } else if (streq(key
, "socket-fd")) {
2605 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2606 log_debug("Failed to parse socket-fd value %s", value
);
2609 if (s
->socket_fd
>= 0)
2610 close_nointr_nofail(s
->socket_fd
);
2611 s
->socket_fd
= fdset_remove(fds
, fd
);
2613 } else if (streq(key
, "main-exec-status-pid")) {
2616 if (parse_pid(value
, &pid
) < 0)
2617 log_debug("Failed to parse main-exec-status-pid value %s", value
);
2619 s
->main_exec_status
.pid
= pid
;
2620 } else if (streq(key
, "main-exec-status-code")) {
2623 if (safe_atoi(value
, &i
) < 0)
2624 log_debug("Failed to parse main-exec-status-code value %s", value
);
2626 s
->main_exec_status
.code
= i
;
2627 } else if (streq(key
, "main-exec-status-status")) {
2630 if (safe_atoi(value
, &i
) < 0)
2631 log_debug("Failed to parse main-exec-status-status value %s", value
);
2633 s
->main_exec_status
.status
= i
;
2634 } else if (streq(key
, "main-exec-status-start"))
2635 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2636 else if (streq(key
, "main-exec-status-exit"))
2637 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2638 else if (streq(key
, "watchdog-timestamp"))
2639 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2641 log_debug("Unknown serialization key '%s'", key
);
2646 static UnitActiveState
service_active_state(Unit
*u
) {
2649 return state_translation_table
[SERVICE(u
)->state
];
2652 static const char *service_sub_state_to_string(Unit
*u
) {
2655 return service_state_to_string(SERVICE(u
)->state
);
2658 static bool service_check_gc(Unit
*u
) {
2659 Service
*s
= SERVICE(u
);
2663 /* Never clean up services that still have a process around,
2664 * even if the service is formally dead. */
2665 if (cgroup_good(s
) > 0 ||
2666 main_pid_good(s
) > 0 ||
2667 control_pid_good(s
) > 0)
2670 #ifdef HAVE_SYSV_COMPAT
2678 static bool service_check_snapshot(Unit
*u
) {
2679 Service
*s
= SERVICE(u
);
2683 return !s
->got_socket_fd
;
2686 static int service_retry_pid_file(Service
*s
) {
2689 assert(s
->pid_file
);
2690 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2692 r
= service_load_pid_file(s
, false);
2696 service_unwatch_pid_file(s
);
2698 service_enter_running(s
, SERVICE_SUCCESS
);
2702 static int service_watch_pid_file(Service
*s
) {
2705 log_debug("Setting watch for %s's PID file %s", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
2706 r
= path_spec_watch(s
->pid_file_pathspec
, UNIT(s
));
2710 /* the pidfile might have appeared just before we set the watch */
2711 service_retry_pid_file(s
);
2715 log_error("Failed to set a watch for %s's PID file %s: %s",
2716 UNIT(s
)->id
, s
->pid_file_pathspec
->path
, strerror(-r
));
2717 service_unwatch_pid_file(s
);
2721 static int service_demand_pid_file(Service
*s
) {
2724 assert(s
->pid_file
);
2725 assert(!s
->pid_file_pathspec
);
2727 ps
= new0(PathSpec
, 1);
2731 ps
->path
= strdup(s
->pid_file
);
2737 path_kill_slashes(ps
->path
);
2739 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2740 * keep their PID file open all the time. */
2741 ps
->type
= PATH_MODIFIED
;
2742 ps
->inotify_fd
= -1;
2744 s
->pid_file_pathspec
= ps
;
2746 return service_watch_pid_file(s
);
2749 static void service_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
2750 Service
*s
= SERVICE(u
);
2754 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2755 assert(s
->pid_file_pathspec
);
2756 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2758 log_debug("inotify event for %s", u
->id
);
2760 if (path_spec_fd_event(s
->pid_file_pathspec
, events
) < 0)
2763 if (service_retry_pid_file(s
) == 0)
2766 if (service_watch_pid_file(s
) < 0)
2771 service_unwatch_pid_file(s
);
2772 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2775 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2776 Service
*s
= SERVICE(u
);
2782 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
) : is_clean_exit_lsb(code
, status
))
2783 f
= SERVICE_SUCCESS
;
2784 else if (code
== CLD_EXITED
)
2785 f
= SERVICE_FAILURE_EXIT_CODE
;
2786 else if (code
== CLD_KILLED
)
2787 f
= SERVICE_FAILURE_SIGNAL
;
2788 else if (code
== CLD_DUMPED
)
2789 f
= SERVICE_FAILURE_CORE_DUMP
;
2791 assert_not_reached("Unknown code");
2793 if (s
->main_pid
== pid
) {
2794 /* Forking services may occasionally move to a new PID.
2795 * As long as they update the PID file before exiting the old
2796 * PID, they're fine. */
2797 if (service_load_pid_file(s
, false) == 0)
2801 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2803 /* If this is not a forking service than the main
2804 * process got started and hence we copy the exit
2805 * status so that it is recorded both as main and as
2806 * control process exit status */
2807 if (s
->main_command
) {
2808 s
->main_command
->exec_status
= s
->main_exec_status
;
2810 if (s
->main_command
->ignore
)
2811 f
= SERVICE_SUCCESS
;
2814 log_full(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2815 "%s: main process exited, code=%s, status=%i", u
->id
, sigchld_code_to_string(code
), status
);
2817 if (f
!= SERVICE_SUCCESS
)
2820 if (s
->main_command
&&
2821 s
->main_command
->command_next
&&
2822 f
== SERVICE_SUCCESS
) {
2824 /* There is another command to *
2825 * execute, so let's do that. */
2827 log_debug("%s running next main command for state %s", u
->id
, service_state_to_string(s
->state
));
2828 service_run_next_main(s
);
2832 /* The service exited, so the service is officially
2834 s
->main_command
= NULL
;
2838 case SERVICE_START_POST
:
2839 case SERVICE_RELOAD
:
2841 /* Need to wait until the operation is
2846 if (s
->type
== SERVICE_ONESHOT
) {
2847 /* This was our main goal, so let's go on */
2848 if (f
== SERVICE_SUCCESS
)
2849 service_enter_start_post(s
);
2851 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2854 assert(s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
);
2859 case SERVICE_RUNNING
:
2860 service_enter_running(s
, f
);
2863 case SERVICE_STOP_SIGTERM
:
2864 case SERVICE_STOP_SIGKILL
:
2866 if (!control_pid_good(s
))
2867 service_enter_stop_post(s
, f
);
2869 /* If there is still a control process, wait for that first */
2873 assert_not_reached("Uh, main process died at wrong time.");
2877 } else if (s
->control_pid
== pid
) {
2881 if (s
->control_command
) {
2882 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2884 if (s
->control_command
->ignore
)
2885 f
= SERVICE_SUCCESS
;
2888 log_full(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2889 "%s: control process exited, code=%s status=%i", u
->id
, sigchld_code_to_string(code
), status
);
2891 if (f
!= SERVICE_SUCCESS
)
2894 if (s
->control_command
&&
2895 s
->control_command
->command_next
&&
2896 f
== SERVICE_SUCCESS
) {
2898 /* There is another command to *
2899 * execute, so let's do that. */
2901 log_debug("%s running next control command for state %s", u
->id
, service_state_to_string(s
->state
));
2902 service_run_next_control(s
);
2905 /* No further commands for this step, so let's
2906 * figure out what to do next */
2908 s
->control_command
= NULL
;
2909 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2911 log_debug("%s got final SIGCHLD for state %s", u
->id
, service_state_to_string(s
->state
));
2915 case SERVICE_START_PRE
:
2916 if (f
== SERVICE_SUCCESS
)
2917 service_enter_start(s
);
2919 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2923 assert(s
->type
== SERVICE_FORKING
);
2925 if (f
!= SERVICE_SUCCESS
) {
2926 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2931 bool has_start_post
;
2934 /* Let's try to load the pid file here if we can.
2935 * The PID file might actually be created by a START_POST
2936 * script. In that case don't worry if the loading fails. */
2938 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2939 r
= service_load_pid_file(s
, !has_start_post
);
2940 if (!has_start_post
&& r
< 0) {
2941 r
= service_demand_pid_file(s
);
2942 if (r
< 0 || !cgroup_good(s
))
2943 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2947 service_search_main_pid(s
);
2949 service_enter_start_post(s
);
2952 case SERVICE_START_POST
:
2953 if (f
!= SERVICE_SUCCESS
) {
2954 service_enter_stop(s
, f
);
2961 r
= service_load_pid_file(s
, true);
2963 r
= service_demand_pid_file(s
);
2964 if (r
< 0 || !cgroup_good(s
))
2965 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2969 service_search_main_pid(s
);
2971 service_enter_running(s
, SERVICE_SUCCESS
);
2974 case SERVICE_RELOAD
:
2975 if (f
== SERVICE_SUCCESS
) {
2976 service_load_pid_file(s
, true);
2977 service_search_main_pid(s
);
2980 s
->reload_result
= f
;
2981 service_enter_running(s
, SERVICE_SUCCESS
);
2985 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2988 case SERVICE_STOP_SIGTERM
:
2989 case SERVICE_STOP_SIGKILL
:
2990 if (main_pid_good(s
) <= 0)
2991 service_enter_stop_post(s
, f
);
2993 /* If there is still a service
2994 * process around, wait until
2995 * that one quit, too */
2998 case SERVICE_STOP_POST
:
2999 case SERVICE_FINAL_SIGTERM
:
3000 case SERVICE_FINAL_SIGKILL
:
3001 service_enter_dead(s
, f
, true);
3005 assert_not_reached("Uh, control process died at wrong time.");
3010 /* Notify clients about changed exit status */
3011 unit_add_to_dbus_queue(u
);
3014 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
3015 Service
*s
= SERVICE(u
);
3018 assert(elapsed
== 1);
3020 if (w
== &s
->watchdog_watch
) {
3021 service_handle_watchdog(s
);
3025 assert(w
== &s
->timer_watch
);
3029 case SERVICE_START_PRE
:
3031 log_warning("%s operation timed out. Terminating.", u
->id
);
3032 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3035 case SERVICE_START_POST
:
3036 log_warning("%s operation timed out. Stopping.", u
->id
);
3037 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
3040 case SERVICE_RELOAD
:
3041 log_warning("%s operation timed out. Stopping.", u
->id
);
3042 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
3043 service_enter_running(s
, SERVICE_SUCCESS
);
3047 log_warning("%s stopping timed out. Terminating.", u
->id
);
3048 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3051 case SERVICE_STOP_SIGTERM
:
3052 if (s
->exec_context
.send_sigkill
) {
3053 log_warning("%s stopping timed out. Killing.", u
->id
);
3054 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
3056 log_warning("%s stopping timed out. Skipping SIGKILL.", u
->id
);
3057 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
3062 case SERVICE_STOP_SIGKILL
:
3063 /* Uh, we sent a SIGKILL and it is still not gone?
3064 * Must be something we cannot kill, so let's just be
3065 * weirded out and continue */
3067 log_warning("%s still around after SIGKILL. Ignoring.", u
->id
);
3068 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
3071 case SERVICE_STOP_POST
:
3072 log_warning("%s stopping timed out (2). Terminating.", u
->id
);
3073 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3076 case SERVICE_FINAL_SIGTERM
:
3077 if (s
->exec_context
.send_sigkill
) {
3078 log_warning("%s stopping timed out (2). Killing.", u
->id
);
3079 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
3081 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u
->id
);
3082 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
3087 case SERVICE_FINAL_SIGKILL
:
3088 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u
->id
);
3089 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
3092 case SERVICE_AUTO_RESTART
:
3093 log_info("%s holdoff time over, scheduling restart.", u
->id
);
3094 service_enter_restart(s
);
3098 assert_not_reached("Timeout at wrong time.");
3102 static void service_cgroup_notify_event(Unit
*u
) {
3103 Service
*s
= SERVICE(u
);
3107 log_debug("%s: cgroup is empty", u
->id
);
3111 /* Waiting for SIGCHLD is usually more interesting,
3112 * because it includes return codes/signals. Which is
3113 * why we ignore the cgroup events for most cases,
3114 * except when we don't know pid which to expect the
3118 case SERVICE_START_POST
:
3119 /* If we were hoping for the daemon to write its PID file,
3120 * we can give up now. */
3121 if (s
->pid_file_pathspec
) {
3122 log_warning("%s never wrote its PID file. Failing.", UNIT(s
)->id
);
3123 service_unwatch_pid_file(s
);
3124 if (s
->state
== SERVICE_START
)
3125 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
3127 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
3131 case SERVICE_RUNNING
:
3132 /* service_enter_running() will figure out what to do */
3133 service_enter_running(s
, SERVICE_SUCCESS
);
3136 case SERVICE_STOP_SIGTERM
:
3137 case SERVICE_STOP_SIGKILL
:
3139 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
3140 service_enter_stop_post(s
, SERVICE_SUCCESS
);
3144 case SERVICE_FINAL_SIGTERM
:
3145 case SERVICE_FINAL_SIGKILL
:
3146 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
3147 service_enter_dead(s
, SERVICE_SUCCESS
, SERVICE_SUCCESS
);
3156 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
) {
3157 Service
*s
= SERVICE(u
);
3162 if (s
->notify_access
== NOTIFY_NONE
) {
3163 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3164 u
->id
, (unsigned long) pid
);
3168 if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
3169 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3170 u
->id
, (unsigned long) pid
, (unsigned long) s
->main_pid
);
3174 log_debug("%s: Got message", u
->id
);
3176 /* Interpret MAINPID= */
3177 if ((e
= strv_find_prefix(tags
, "MAINPID=")) &&
3178 (s
->state
== SERVICE_START
||
3179 s
->state
== SERVICE_START_POST
||
3180 s
->state
== SERVICE_RUNNING
||
3181 s
->state
== SERVICE_RELOAD
)) {
3183 if (parse_pid(e
+ 8, &pid
) < 0)
3184 log_warning("Failed to parse notification message %s", e
);
3186 log_debug("%s: got %s", u
->id
, e
);
3187 service_set_main_pid(s
, pid
);
3191 /* Interpret READY= */
3192 if (s
->type
== SERVICE_NOTIFY
&&
3193 s
->state
== SERVICE_START
&&
3194 strv_find(tags
, "READY=1")) {
3195 log_debug("%s: got READY=1", u
->id
);
3197 service_enter_start_post(s
);
3200 /* Interpret STATUS= */
3201 if ((e
= strv_find_prefix(tags
, "STATUS="))) {
3205 if (!(t
= strdup(e
+7))) {
3206 log_error("Failed to allocate string.");
3210 log_debug("%s: got %s", u
->id
, e
);
3212 free(s
->status_text
);
3215 free(s
->status_text
);
3216 s
->status_text
= NULL
;
3220 if (strv_find(tags
, "WATCHDOG=1")) {
3221 log_debug("%s: got WATCHDOG=1", u
->id
);
3222 service_reset_watchdog(s
);
3225 /* Notify clients about changed status or main pid */
3226 unit_add_to_dbus_queue(u
);
3229 #ifdef HAVE_SYSV_COMPAT
3232 static void sysv_facility_in_insserv_conf(Manager
*mgr
) {
3236 if (!(f
= fopen("/etc/insserv.conf", "re"))) {
3237 r
= errno
== ENOENT
? 0 : -errno
;
3242 char l
[LINE_MAX
], *t
;
3243 char **parsed
= NULL
;
3245 if (!fgets(l
, sizeof(l
), f
)) {
3250 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r
));
3255 if (*t
!= '$' && *t
!= '<')
3258 parsed
= strv_split(t
,WHITESPACE
);
3259 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3260 if (parsed
&& !startswith_no_case (parsed
[0], "<interactive>")) {
3263 if (sysv_translate_facility(parsed
[0], NULL
, &facility
) < 0)
3265 if ((u
= manager_get_unit(mgr
, facility
)) && (u
->type
== UNIT_TARGET
)) {
3267 char *dep
= NULL
, *name
, **j
;
3269 STRV_FOREACH (j
, parsed
+1) {
3278 if (sysv_translate_facility(name
, NULL
, &dep
) < 0)
3281 r
= unit_add_two_dependencies_by_name(u
, UNIT_BEFORE
, e
, dep
, NULL
, true);
3296 static int service_enumerate(Manager
*m
) {
3300 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
3301 Set
*runlevel_services
[ELEMENTSOF(rcnd_table
)], *shutdown_services
= NULL
;
3308 if (m
->running_as
!= MANAGER_SYSTEM
)
3311 zero(runlevel_services
);
3313 STRV_FOREACH(p
, m
->lookup_paths
.sysvrcnd_path
)
3314 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++) {
3318 path
= join(*p
, "/", rcnd_table
[i
].path
, NULL
);
3327 if (!(d
= opendir(path
))) {
3328 if (errno
!= ENOENT
)
3329 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
3334 while ((de
= readdir(d
))) {
3337 if (ignore_file(de
->d_name
))
3340 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
3343 if (strlen(de
->d_name
) < 4)
3346 a
= undecchar(de
->d_name
[1]);
3347 b
= undecchar(de
->d_name
[2]);
3353 fpath
= join(path
, "/", de
->d_name
, NULL
);
3359 if (access(fpath
, X_OK
) < 0) {
3361 if (errno
!= ENOENT
)
3362 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
3368 if (!(name
= sysv_translate_name(de
->d_name
+ 3))) {
3373 if ((r
= manager_load_unit_prepare(m
, name
, NULL
, NULL
, &service
)) < 0) {
3374 log_warning("Failed to prepare unit %s: %s", name
, strerror(-r
));
3378 if (de
->d_name
[0] == 'S') {
3380 if (rcnd_table
[i
].type
== RUNLEVEL_UP
|| rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
) {
3381 SERVICE(service
)->sysv_start_priority_from_rcnd
=
3382 MAX(a
*10 + b
, SERVICE(service
)->sysv_start_priority_from_rcnd
);
3384 SERVICE(service
)->sysv_enabled
= true;
3387 if ((r
= set_ensure_allocated(&runlevel_services
[i
], trivial_hash_func
, trivial_compare_func
)) < 0)
3390 if ((r
= set_put(runlevel_services
[i
], service
)) < 0)
3393 } else if (de
->d_name
[0] == 'K' &&
3394 (rcnd_table
[i
].type
== RUNLEVEL_DOWN
||
3395 rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
)) {
3397 if ((r
= set_ensure_allocated(&shutdown_services
, trivial_hash_func
, trivial_compare_func
)) < 0)
3400 if ((r
= set_put(shutdown_services
, service
)) < 0)
3406 /* Now we loaded all stubs and are aware of the lowest
3407 start-up priority for all services, not let's actually load
3408 the services, this will also tell us which services are
3409 actually native now */
3410 manager_dispatch_load_queue(m
);
3412 /* If this is a native service, rely on native ways to pull in
3413 * a service, don't pull it in via sysv rcN.d links. */
3414 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
3415 SET_FOREACH(service
, runlevel_services
[i
], j
) {
3416 service
= unit_follow_merge(service
);
3418 if (service
->fragment_path
)
3421 if ((r
= unit_add_two_dependencies_by_name_inverse(service
, UNIT_AFTER
, UNIT_WANTS
, rcnd_table
[i
].target
, NULL
, true)) < 0)
3425 /* We honour K links only for halt/reboot. For the normal
3426 * runlevels we assume the stop jobs will be implicitly added
3427 * by the core logic. Also, we don't really distinguish here
3428 * between the runlevels 0 and 6 and just add them to the
3429 * special shutdown target. On SUSE the boot.d/ runlevel is
3430 * also used for shutdown, so we add links for that too to the
3431 * shutdown target.*/
3432 SET_FOREACH(service
, shutdown_services
, j
) {
3433 service
= unit_follow_merge(service
);
3435 if (service
->fragment_path
)
3438 if ((r
= unit_add_two_dependencies_by_name(service
, UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true)) < 0)
3445 sysv_facility_in_insserv_conf (m
);
3453 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
3454 set_free(runlevel_services
[i
]);
3455 set_free(shutdown_services
);
3464 static void service_bus_name_owner_change(
3467 const char *old_owner
,
3468 const char *new_owner
) {
3470 Service
*s
= SERVICE(u
);
3475 assert(streq(s
->bus_name
, name
));
3476 assert(old_owner
|| new_owner
);
3478 if (old_owner
&& new_owner
)
3479 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->id
, name
, old_owner
, new_owner
);
3481 log_debug("%s's D-Bus name %s no longer registered by %s", u
->id
, name
, old_owner
);
3483 log_debug("%s's D-Bus name %s now registered by %s", u
->id
, name
, new_owner
);
3485 s
->bus_name_good
= !!new_owner
;
3487 if (s
->type
== SERVICE_DBUS
) {
3489 /* service_enter_running() will figure out what to
3491 if (s
->state
== SERVICE_RUNNING
)
3492 service_enter_running(s
, SERVICE_SUCCESS
);
3493 else if (s
->state
== SERVICE_START
&& new_owner
)
3494 service_enter_start_post(s
);
3496 } else if (new_owner
&&
3498 (s
->state
== SERVICE_START
||
3499 s
->state
== SERVICE_START_POST
||
3500 s
->state
== SERVICE_RUNNING
||
3501 s
->state
== SERVICE_RELOAD
)) {
3503 /* Try to acquire PID from bus service */
3504 log_debug("Trying to acquire PID from D-Bus name...");
3506 bus_query_pid(u
->manager
, name
);
3510 static void service_bus_query_pid_done(
3515 Service
*s
= SERVICE(u
);
3520 log_debug("%s's D-Bus name %s is now owned by process %u", u
->id
, name
, (unsigned) pid
);
3522 if (s
->main_pid
<= 0 &&
3523 (s
->state
== SERVICE_START
||
3524 s
->state
== SERVICE_START_POST
||
3525 s
->state
== SERVICE_RUNNING
||
3526 s
->state
== SERVICE_RELOAD
))
3527 service_set_main_pid(s
, pid
);
3530 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
) {
3535 /* This is called by the socket code when instantiating a new
3536 * service for a stream socket and the socket needs to be
3539 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3542 if (s
->socket_fd
>= 0)
3545 if (s
->state
!= SERVICE_DEAD
)
3549 s
->got_socket_fd
= true;
3551 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3553 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3556 static void service_reset_failed(Unit
*u
) {
3557 Service
*s
= SERVICE(u
);
3561 if (s
->state
== SERVICE_FAILED
)
3562 service_set_state(s
, SERVICE_DEAD
);
3564 s
->result
= SERVICE_SUCCESS
;
3565 s
->reload_result
= SERVICE_SUCCESS
;
3568 static bool service_need_daemon_reload(Unit
*u
) {
3569 Service
*s
= SERVICE(u
);
3573 #ifdef HAVE_SYSV_COMPAT
3578 if (stat(s
->sysv_path
, &st
) < 0)
3579 /* What, cannot access this anymore? */
3582 if (s
->sysv_mtime
> 0 &&
3583 timespec_load(&st
.st_mtim
) != s
->sysv_mtime
)
3591 static int service_kill(Unit
*u
, KillWho who
, KillMode mode
, int signo
, DBusError
*error
) {
3592 Service
*s
= SERVICE(u
);
3594 Set
*pid_set
= NULL
;
3598 if (s
->main_pid
<= 0 && who
== KILL_MAIN
) {
3599 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No main process to kill");
3603 if (s
->control_pid
<= 0 && who
== KILL_CONTROL
) {
3604 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No control process to kill");
3608 if (who
== KILL_CONTROL
|| who
== KILL_ALL
)
3609 if (s
->control_pid
> 0)
3610 if (kill(s
->control_pid
, signo
) < 0)
3613 if (who
== KILL_MAIN
|| who
== KILL_ALL
)
3614 if (s
->main_pid
> 0)
3615 if (kill(s
->main_pid
, signo
) < 0)
3618 if (who
== KILL_ALL
&& mode
== KILL_CONTROL_GROUP
) {
3621 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
)))
3624 /* Exclude the control/main pid from being killed via the cgroup */
3625 if (s
->control_pid
> 0)
3626 if ((q
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0) {
3631 if (s
->main_pid
> 0)
3632 if ((q
= set_put(pid_set
, LONG_TO_PTR(s
->main_pid
))) < 0) {
3637 if ((q
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, signo
, false, pid_set
)) < 0)
3638 if (q
!= -EAGAIN
&& q
!= -ESRCH
&& q
!= -ENOENT
)
3649 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
3650 [SERVICE_DEAD
] = "dead",
3651 [SERVICE_START_PRE
] = "start-pre",
3652 [SERVICE_START
] = "start",
3653 [SERVICE_START_POST
] = "start-post",
3654 [SERVICE_RUNNING
] = "running",
3655 [SERVICE_EXITED
] = "exited",
3656 [SERVICE_RELOAD
] = "reload",
3657 [SERVICE_STOP
] = "stop",
3658 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
3659 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
3660 [SERVICE_STOP_POST
] = "stop-post",
3661 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
3662 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
3663 [SERVICE_FAILED
] = "failed",
3664 [SERVICE_AUTO_RESTART
] = "auto-restart",
3667 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
3669 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3670 [SERVICE_RESTART_NO
] = "no",
3671 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3672 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3673 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3674 [SERVICE_RESTART_ALWAYS
] = "always"
3677 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3679 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3680 [SERVICE_SIMPLE
] = "simple",
3681 [SERVICE_FORKING
] = "forking",
3682 [SERVICE_ONESHOT
] = "oneshot",
3683 [SERVICE_DBUS
] = "dbus",
3684 [SERVICE_NOTIFY
] = "notify"
3687 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3689 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3690 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3691 [SERVICE_EXEC_START
] = "ExecStart",
3692 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3693 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3694 [SERVICE_EXEC_STOP
] = "ExecStop",
3695 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3698 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3700 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3701 [NOTIFY_NONE
] = "none",
3702 [NOTIFY_MAIN
] = "main",
3703 [NOTIFY_ALL
] = "all"
3706 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3708 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3709 [SERVICE_SUCCESS
] = "success",
3710 [SERVICE_FAILURE_RESOURCES
] = "resources",
3711 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3712 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3713 [SERVICE_FAILURE_SIGNAL
] = "signal",
3714 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3715 [SERVICE_FAILURE_WATCHDOG
] = "watchdog"
3718 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3720 static const char* const start_limit_action_table
[_SERVICE_START_LIMIT_MAX
] = {
3721 [SERVICE_START_LIMIT_NONE
] = "none",
3722 [SERVICE_START_LIMIT_REBOOT
] = "reboot",
3723 [SERVICE_START_LIMIT_REBOOT_FORCE
] = "reboot-force",
3724 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE
] = "reboot-immediate"
3726 DEFINE_STRING_TABLE_LOOKUP(start_limit_action
, StartLimitAction
);
3728 const UnitVTable service_vtable
= {
3729 .suffix
= ".service",
3730 .object_size
= sizeof(Service
),
3735 .show_status
= true,
3737 .init
= service_init
,
3738 .done
= service_done
,
3739 .load
= service_load
,
3741 .coldplug
= service_coldplug
,
3743 .dump
= service_dump
,
3745 .start
= service_start
,
3746 .stop
= service_stop
,
3747 .reload
= service_reload
,
3749 .can_reload
= service_can_reload
,
3751 .kill
= service_kill
,
3753 .serialize
= service_serialize
,
3754 .deserialize_item
= service_deserialize_item
,
3756 .active_state
= service_active_state
,
3757 .sub_state_to_string
= service_sub_state_to_string
,
3759 .check_gc
= service_check_gc
,
3760 .check_snapshot
= service_check_snapshot
,
3762 .sigchld_event
= service_sigchld_event
,
3763 .timer_event
= service_timer_event
,
3764 .fd_event
= service_fd_event
,
3766 .reset_failed
= service_reset_failed
,
3768 .need_daemon_reload
= service_need_daemon_reload
,
3770 .cgroup_notify_empty
= service_cgroup_notify_event
,
3771 .notify_message
= service_notify_message
,
3773 .bus_name_owner_change
= service_bus_name_owner_change
,
3774 .bus_query_pid_done
= service_bus_query_pid_done
,
3776 .bus_interface
= "org.freedesktop.systemd1.Service",
3777 .bus_message_handler
= bus_service_message_handler
,
3778 .bus_invalidating_properties
= bus_service_invalidating_properties
,
3780 #ifdef HAVE_SYSV_COMPAT
3781 .enumerate
= service_enumerate