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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser 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 "unit-printf.h"
37 #include "dbus-service.h"
39 #include "bus-errors.h"
40 #include "exit-status.h"
42 #include "path-util.h"
46 #ifdef HAVE_SYSV_COMPAT
48 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
50 typedef enum RunlevelType
{
58 const RunlevelType type
;
60 /* Standard SysV runlevels for start-up */
61 { "rc1.d", SPECIAL_RESCUE_TARGET
, RUNLEVEL_UP
},
62 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET
, RUNLEVEL_UP
},
63 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET
, RUNLEVEL_UP
},
64 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET
, RUNLEVEL_UP
},
65 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET
, RUNLEVEL_UP
},
67 /* Standard SysV runlevels for shutdown */
68 { "rc0.d", SPECIAL_POWEROFF_TARGET
, RUNLEVEL_DOWN
},
69 { "rc6.d", SPECIAL_REBOOT_TARGET
, RUNLEVEL_DOWN
}
71 /* Note that the order here matters, as we read the
72 directories in this order, and we want to make sure that
73 sysv_start_priority is known when we first load the
74 unit. And that value we only know from S links. Hence
75 UP must be read before DOWN */
78 #define RUNLEVELS_UP "12345"
81 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
82 [SERVICE_DEAD
] = UNIT_INACTIVE
,
83 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
84 [SERVICE_START
] = UNIT_ACTIVATING
,
85 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
86 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
87 [SERVICE_EXITED
] = UNIT_ACTIVE
,
88 [SERVICE_RELOAD
] = UNIT_RELOADING
,
89 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
90 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
91 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
92 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
93 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
94 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
95 [SERVICE_FAILED
] = UNIT_FAILED
,
96 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
99 /* For Type=idle we never want to delay any other jobs, hence we
100 * consider idle jobs active as soon as we start working on them */
101 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
102 [SERVICE_DEAD
] = UNIT_INACTIVE
,
103 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
104 [SERVICE_START
] = UNIT_ACTIVE
,
105 [SERVICE_START_POST
] = UNIT_ACTIVE
,
106 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
107 [SERVICE_EXITED
] = UNIT_ACTIVE
,
108 [SERVICE_RELOAD
] = UNIT_RELOADING
,
109 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
110 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
111 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
112 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
113 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
114 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
115 [SERVICE_FAILED
] = UNIT_FAILED
,
116 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
119 static void service_init(Unit
*u
) {
120 Service
*s
= SERVICE(u
);
123 assert(u
->load_state
== UNIT_STUB
);
125 s
->timeout_start_usec
= DEFAULT_TIMEOUT_USEC
;
126 s
->timeout_stop_usec
= DEFAULT_TIMEOUT_USEC
;
127 s
->restart_usec
= DEFAULT_RESTART_USEC
;
128 s
->type
= _SERVICE_TYPE_INVALID
;
130 watch_init(&s
->watchdog_watch
);
131 watch_init(&s
->timer_watch
);
133 #ifdef HAVE_SYSV_COMPAT
134 s
->sysv_start_priority
= -1;
135 s
->sysv_start_priority_from_rcnd
= -1;
138 s
->guess_main_pid
= true;
140 exec_context_init(&s
->exec_context
);
141 kill_context_init(&s
->kill_context
);
143 RATELIMIT_INIT(s
->start_limit
, 10*USEC_PER_SEC
, 5);
145 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
148 static void service_unwatch_control_pid(Service
*s
) {
151 if (s
->control_pid
<= 0)
154 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
158 static void service_unwatch_main_pid(Service
*s
) {
161 if (s
->main_pid
<= 0)
164 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
168 static void service_unwatch_pid_file(Service
*s
) {
169 if (!s
->pid_file_pathspec
)
172 log_debug("Stopping watch for %s's PID file %s", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
173 path_spec_unwatch(s
->pid_file_pathspec
, UNIT(s
));
174 path_spec_done(s
->pid_file_pathspec
);
175 free(s
->pid_file_pathspec
);
176 s
->pid_file_pathspec
= NULL
;
179 static int service_set_main_pid(Service
*s
, pid_t pid
) {
191 s
->main_pid_known
= true;
193 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
194 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
195 UNIT(s
)->id
, (unsigned long) pid
);
197 s
->main_pid_alien
= true;
199 s
->main_pid_alien
= false;
201 exec_status_start(&s
->main_exec_status
, pid
);
206 static void service_close_socket_fd(Service
*s
) {
209 if (s
->socket_fd
< 0)
212 close_nointr_nofail(s
->socket_fd
);
216 static void service_connection_unref(Service
*s
) {
219 if (!UNIT_DEREF(s
->accept_socket
))
222 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
223 unit_ref_unset(&s
->accept_socket
);
226 static void service_stop_watchdog(Service
*s
) {
229 unit_unwatch_timer(UNIT(s
), &s
->watchdog_watch
);
230 s
->watchdog_timestamp
.realtime
= 0;
231 s
->watchdog_timestamp
.monotonic
= 0;
234 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
);
236 static void service_handle_watchdog(Service
*s
) {
242 if (s
->watchdog_usec
== 0)
245 offset
= now(CLOCK_MONOTONIC
) - s
->watchdog_timestamp
.monotonic
;
246 if (offset
>= s
->watchdog_usec
) {
247 log_error("%s watchdog timeout!", UNIT(s
)->id
);
248 service_enter_dead(s
, SERVICE_FAILURE_WATCHDOG
, true);
252 r
= unit_watch_timer(UNIT(s
), CLOCK_MONOTONIC
, true, s
->watchdog_usec
- offset
, &s
->watchdog_watch
);
254 log_warning("%s failed to install watchdog timer: %s", UNIT(s
)->id
, strerror(-r
));
257 static void service_reset_watchdog(Service
*s
) {
260 dual_timestamp_get(&s
->watchdog_timestamp
);
261 service_handle_watchdog(s
);
264 static void service_done(Unit
*u
) {
265 Service
*s
= SERVICE(u
);
272 #ifdef HAVE_SYSV_COMPAT
273 free(s
->sysv_runlevels
);
274 s
->sysv_runlevels
= NULL
;
277 free(s
->status_text
);
278 s
->status_text
= NULL
;
280 exec_context_done(&s
->exec_context
);
281 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
282 s
->control_command
= NULL
;
283 s
->main_command
= NULL
;
285 set_free(s
->restart_ignore_status
.code
);
286 s
->restart_ignore_status
.code
= NULL
;
287 set_free(s
->restart_ignore_status
.signal
);
288 s
->restart_ignore_status
.signal
= NULL
;
290 set_free(s
->success_status
.code
);
291 s
->success_status
.code
= NULL
;
292 set_free(s
->success_status
.signal
);
293 s
->success_status
.signal
= NULL
;
295 /* This will leak a process, but at least no memory or any of
297 service_unwatch_main_pid(s
);
298 service_unwatch_control_pid(s
);
299 service_unwatch_pid_file(s
);
302 unit_unwatch_bus_name(u
, s
->bus_name
);
307 service_close_socket_fd(s
);
308 service_connection_unref(s
);
310 unit_ref_unset(&s
->accept_socket
);
312 service_stop_watchdog(s
);
314 unit_unwatch_timer(u
, &s
->timer_watch
);
317 #ifdef HAVE_SYSV_COMPAT
318 static char *sysv_translate_name(const char *name
) {
321 if (!(r
= new(char, strlen(name
) + sizeof(".service"))))
324 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
325 if (endswith(name
, ".sh"))
326 /* Drop Debian-style .sh suffix */
327 strcpy(stpcpy(r
, name
) - 3, ".service");
329 #ifdef TARGET_FRUGALWARE
330 if (startswith(name
, "rc."))
331 /* Drop Frugalware-style rc. prefix */
332 strcpy(stpcpy(r
, name
+ 3), ".service");
335 /* Normal init scripts */
336 strcpy(stpcpy(r
, name
), ".service");
341 static int sysv_translate_facility(const char *name
, const char *filename
, char **_r
) {
343 /* We silently ignore the $ prefix here. According to the LSB
344 * spec it simply indicates whether something is a
345 * standardized name or a distribution-specific one. Since we
346 * just follow what already exists and do not introduce new
347 * uses or names we don't care who introduced a new name. */
349 static const char * const table
[] = {
350 /* LSB defined facilities */
351 "local_fs", SPECIAL_LOCAL_FS_TARGET
,
352 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
354 /* Due to unfortunate name selection in Mandriva,
355 * $network is provided by network-up which is ordered
356 * after network which actually starts interfaces.
357 * To break the loop, just ignore it */
358 "network", SPECIAL_NETWORK_TARGET
,
360 "named", SPECIAL_NSS_LOOKUP_TARGET
,
361 "portmap", SPECIAL_RPCBIND_TARGET
,
362 "remote_fs", SPECIAL_REMOTE_FS_TARGET
,
363 "syslog", SPECIAL_SYSLOG_TARGET
,
364 "time", SPECIAL_TIME_SYNC_TARGET
,
366 /* common extensions */
367 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
368 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
,
371 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
372 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
376 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
387 n
= *name
== '$' ? name
+ 1 : name
;
389 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2) {
391 if (!streq(table
[i
], n
))
397 if (!(r
= strdup(table
[i
+1])))
403 /* If we don't know this name, fallback heuristics to figure
404 * out whether something is a target or a service alias. */
407 if (!unit_prefix_is_valid(n
))
410 /* Facilities starting with $ are most likely targets */
411 r
= unit_name_build(n
, NULL
, ".target");
412 } else if (filename
&& streq(name
, filename
))
413 /* Names equaling the file name of the services are redundant */
416 /* Everything else we assume to be normal service names */
417 r
= sysv_translate_name(n
);
428 static int sysv_fix_order(Service
*s
) {
434 if (s
->sysv_start_priority
< 0)
437 /* For each pair of services where at least one lacks a LSB
438 * header, we use the start priority value to order things. */
440 LIST_FOREACH(units_by_type
, other
, UNIT(s
)->manager
->units_by_type
[UNIT_SERVICE
]) {
443 bool special_s
, special_t
;
450 if (UNIT(t
)->load_state
!= UNIT_LOADED
)
453 if (t
->sysv_start_priority
< 0)
456 /* If both units have modern headers we don't care
457 * about the priorities */
458 if ((UNIT(s
)->fragment_path
|| s
->sysv_has_lsb
) &&
459 (UNIT(t
)->fragment_path
|| t
->sysv_has_lsb
))
462 special_s
= s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
);
463 special_t
= t
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, t
->sysv_runlevels
);
465 if (special_t
&& !special_s
)
467 else if (special_s
&& !special_t
)
469 else if (t
->sysv_start_priority
< s
->sysv_start_priority
)
471 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
476 /* FIXME: Maybe we should compare the name here lexicographically? */
478 if ((r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
485 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
488 if (!(c
= new0(ExecCommand
, 1)))
491 if (!(c
->path
= strdup(path
))) {
496 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
505 static int sysv_exec_commands(Service
*s
, const bool supports_reload
) {
510 assert(UNIT(s
)->source_path
);
512 c
= exec_command_new(UNIT(s
)->source_path
, "start");
515 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
517 c
= exec_command_new(UNIT(s
)->source_path
, "stop");
520 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
522 if (supports_reload
) {
523 c
= exec_command_new(UNIT(s
)->source_path
, "reload");
526 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
532 static bool usage_contains_reload(const char *line
) {
533 return (strcasestr(line
, "{reload|") ||
534 strcasestr(line
, "{reload}") ||
535 strcasestr(line
, "{reload\"") ||
536 strcasestr(line
, "|reload|") ||
537 strcasestr(line
, "|reload}") ||
538 strcasestr(line
, "|reload\""));
541 static int service_load_sysv_path(Service
*s
, const char *path
) {
553 char *short_description
= NULL
, *long_description
= NULL
, *chkconfig_description
= NULL
, *description
;
555 bool supports_reload
= false;
562 f
= fopen(path
, "re");
564 r
= errno
== ENOENT
? 0 : -errno
;
568 if (fstat(fileno(f
), &st
) < 0) {
573 free(u
->source_path
);
574 u
->source_path
= strdup(path
);
575 if (!u
->source_path
) {
579 u
->source_mtime
= timespec_load(&st
.st_mtim
);
581 if (null_or_empty(&st
)) {
582 u
->load_state
= UNIT_MASKED
;
590 char l
[LINE_MAX
], *t
;
592 if (!fgets(l
, sizeof(l
), f
)) {
597 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
605 /* Try to figure out whether this init script supports
606 * the reload operation. This heuristic looks for
607 * "Usage" lines which include the reload option. */
608 if ( state
== USAGE_CONTINUATION
||
609 (state
== NORMAL
&& strcasestr(t
, "usage"))) {
610 if (usage_contains_reload(t
)) {
611 supports_reload
= true;
613 } else if (t
[strlen(t
)-1] == '\\')
614 state
= USAGE_CONTINUATION
;
622 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
624 s
->sysv_has_lsb
= true;
628 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
634 t
+= strspn(t
, WHITESPACE
);
636 if (state
== NORMAL
) {
638 /* Try to parse Red Hat style chkconfig headers */
640 if (startswith_no_case(t
, "chkconfig:")) {
642 char runlevels
[16], *k
;
646 if (sscanf(t
+10, "%15s %i %*i",
648 &start_priority
) != 2) {
650 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
654 /* A start priority gathered from the
655 * symlink farms is preferred over the
656 * data from the LSB header. */
657 if (start_priority
< 0 || start_priority
> 99)
658 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
660 s
->sysv_start_priority
= start_priority
;
662 char_array_0(runlevels
);
663 k
= delete_chars(runlevels
, WHITESPACE
"-");
668 if (!(d
= strdup(k
))) {
673 free(s
->sysv_runlevels
);
674 s
->sysv_runlevels
= d
;
677 } else if (startswith_no_case(t
, "description:")) {
679 size_t k
= strlen(t
);
683 if (t
[k
-1] == '\\') {
688 if ((j
= strstrip(t
+12)) && *j
) {
689 if (!(d
= strdup(j
))) {
696 free(chkconfig_description
);
697 chkconfig_description
= d
;
699 } else if (startswith_no_case(t
, "pidfile:")) {
706 if (!path_is_absolute(fn
)) {
707 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
711 if (!(fn
= strdup(fn
))) {
720 } else if (state
== DESCRIPTION
) {
722 /* Try to parse Red Hat style description
725 size_t k
= strlen(t
);
733 if ((j
= strstrip(t
)) && *j
) {
736 if (chkconfig_description
)
737 d
= strjoin(chkconfig_description
, " ", j
, NULL
);
746 free(chkconfig_description
);
747 chkconfig_description
= d
;
750 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
752 if (startswith_no_case(t
, "Provides:")) {
758 FOREACH_WORD_QUOTED(w
, z
, t
+9, i
) {
761 if (!(n
= strndup(w
, z
))) {
766 r
= sysv_translate_facility(n
, path_get_file_name(path
), &m
);
775 if (unit_name_to_type(m
) == UNIT_SERVICE
)
776 r
= unit_add_name(u
, m
);
783 * indication that the
785 * now available. This
788 * targets do NOT pull
791 r
= unit_add_two_dependencies_by_name(u
, UNIT_BEFORE
, UNIT_WANTS
, m
, NULL
, true);
794 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path
, line
, m
, strerror(-r
));
799 } else if (startswith_no_case(t
, "Required-Start:") ||
800 startswith_no_case(t
, "Should-Start:") ||
801 startswith_no_case(t
, "X-Start-Before:") ||
802 startswith_no_case(t
, "X-Start-After:")) {
808 FOREACH_WORD_QUOTED(w
, z
, strchr(t
, ':')+1, i
) {
811 if (!(n
= strndup(w
, z
))) {
816 r
= sysv_translate_facility(n
, path_get_file_name(path
), &m
);
819 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path
, line
, n
, strerror(-r
));
829 r
= unit_add_dependency_by_name(u
, startswith_no_case(t
, "X-Start-Before:") ? UNIT_BEFORE
: UNIT_AFTER
, m
, NULL
, true);
832 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path
, line
, m
, strerror(-r
));
836 } else if (startswith_no_case(t
, "Default-Start:")) {
841 k
= delete_chars(t
+14, WHITESPACE
"-");
844 if (!(d
= strdup(k
))) {
849 free(s
->sysv_runlevels
);
850 s
->sysv_runlevels
= d
;
853 } else if (startswith_no_case(t
, "Description:")) {
856 state
= LSB_DESCRIPTION
;
858 if ((j
= strstrip(t
+12)) && *j
) {
859 if (!(d
= strdup(j
))) {
866 free(long_description
);
867 long_description
= d
;
869 } else if (startswith_no_case(t
, "Short-Description:")) {
874 if ((j
= strstrip(t
+18)) && *j
) {
875 if (!(d
= strdup(j
))) {
882 free(short_description
);
883 short_description
= d
;
885 } else if (state
== LSB_DESCRIPTION
) {
887 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
890 if ((j
= strstrip(t
)) && *j
) {
893 if (long_description
)
894 d
= strjoin(long_description
, " ", t
, NULL
);
903 free(long_description
);
904 long_description
= d
;
913 if ((r
= sysv_exec_commands(s
, supports_reload
)) < 0)
916 if (s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
)) {
917 /* If there a runlevels configured for this service
918 * but none of the standard ones, then we assume this
919 * is some special kind of service (which might be
920 * needed for early boot) and don't create any links
923 UNIT(s
)->default_dependencies
= false;
925 /* Don't timeout special services during boot (like fsck) */
926 s
->timeout_start_usec
= 0;
927 s
->timeout_stop_usec
= 0;
929 s
->timeout_start_usec
= DEFAULT_SYSV_TIMEOUT_USEC
;
930 s
->timeout_stop_usec
= DEFAULT_SYSV_TIMEOUT_USEC
;
933 /* Special setting for all SysV services */
934 s
->type
= SERVICE_FORKING
;
935 s
->remain_after_exit
= !s
->pid_file
;
936 s
->guess_main_pid
= false;
937 s
->restart
= SERVICE_RESTART_NO
;
938 s
->exec_context
.ignore_sigpipe
= false;
939 s
->kill_context
.kill_mode
= KILL_PROCESS
;
941 /* We use the long description only if
942 * no short description is set. */
944 if (short_description
)
945 description
= short_description
;
946 else if (chkconfig_description
)
947 description
= chkconfig_description
;
948 else if (long_description
)
949 description
= long_description
;
956 if (!(d
= strappend(s
->sysv_has_lsb
? "LSB: " : "SYSV: ", description
))) {
964 /* The priority that has been set in /etc/rcN.d/ hierarchies
965 * takes precedence over what is stored as default in the LSB
967 if (s
->sysv_start_priority_from_rcnd
>= 0)
968 s
->sysv_start_priority
= s
->sysv_start_priority_from_rcnd
;
970 u
->load_state
= UNIT_LOADED
;
978 free(short_description
);
979 free(long_description
);
980 free(chkconfig_description
);
985 static int service_load_sysv_name(Service
*s
, const char *name
) {
991 /* For SysV services we strip the rc.* and *.sh
992 * prefixes/suffixes. */
993 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
994 if (endswith(name
, ".sh.service"))
998 #ifdef TARGET_FRUGALWARE
999 if (startswith(name
, "rc."))
1003 STRV_FOREACH(p
, UNIT(s
)->manager
->lookup_paths
.sysvinit_path
) {
1007 path
= strjoin(*p
, "/", name
, NULL
);
1011 assert(endswith(path
, ".service"));
1012 path
[strlen(path
)-8] = 0;
1014 r
= service_load_sysv_path(s
, path
);
1016 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
1017 if (r
>= 0 && UNIT(s
)->load_state
== UNIT_STUB
) {
1018 /* Try Debian style *.sh source'able init scripts */
1019 strcat(path
, ".sh");
1020 r
= service_load_sysv_path(s
, path
);
1025 #ifdef TARGET_FRUGALWARE
1026 if (r
>= 0 && UNIT(s
)->load_state
== UNIT_STUB
) {
1027 /* Try Frugalware style rc.* init scripts */
1029 path
= strjoin(*p
, "/rc.", name
, NULL
);
1033 /* Drop .service suffix */
1034 path
[strlen(path
)-8] = 0;
1035 r
= service_load_sysv_path(s
, path
);
1043 if ((UNIT(s
)->load_state
!= UNIT_STUB
))
1050 static int service_load_sysv(Service
*s
) {
1057 /* Load service data from SysV init scripts, preferably with
1058 * LSB headers ... */
1060 if (strv_isempty(UNIT(s
)->manager
->lookup_paths
.sysvinit_path
))
1063 if ((t
= UNIT(s
)->id
))
1064 if ((r
= service_load_sysv_name(s
, t
)) < 0)
1067 if (UNIT(s
)->load_state
== UNIT_STUB
)
1068 SET_FOREACH(t
, UNIT(s
)->names
, i
) {
1069 if (t
== UNIT(s
)->id
)
1072 if ((r
= service_load_sysv_name(s
, t
)) < 0)
1075 if (UNIT(s
)->load_state
!= UNIT_STUB
)
1083 static int fsck_fix_order(Service
*s
) {
1089 if (s
->fsck_passno
<= 0)
1092 /* For each pair of services where both have an fsck priority
1093 * we order things based on it. */
1095 LIST_FOREACH(units_by_type
, other
, UNIT(s
)->manager
->units_by_type
[UNIT_SERVICE
]) {
1104 if (UNIT(t
)->load_state
!= UNIT_LOADED
)
1107 if (t
->fsck_passno
<= 0)
1110 if (t
->fsck_passno
< s
->fsck_passno
)
1112 else if (t
->fsck_passno
> s
->fsck_passno
)
1117 if ((r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
1124 static int service_verify(Service
*s
) {
1127 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
1130 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
1131 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s
)->id
);
1135 if (s
->type
!= SERVICE_ONESHOT
&&
1136 s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
1137 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
1141 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
1142 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s
)->id
);
1146 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
1147 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s
)->id
);
1149 if (s
->exec_context
.pam_name
&& s
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
1150 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s
)->id
);
1157 static int service_add_default_dependencies(Service
*s
) {
1162 /* Add a number of automatic dependencies useful for the
1163 * majority of services. */
1165 /* First, pull in base system */
1166 if (UNIT(s
)->manager
->running_as
== SYSTEMD_SYSTEM
) {
1168 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
1171 } else if (UNIT(s
)->manager
->running_as
== SYSTEMD_USER
) {
1173 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SOCKETS_TARGET
, NULL
, true)) < 0)
1177 /* Second, activate normal shutdown */
1178 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
1181 static void service_fix_output(Service
*s
) {
1184 /* If nothing has been explicitly configured, patch default
1185 * output in. If input is socket/tty we avoid this however,
1186 * since in that case we want output to default to the same
1187 * place as we read input from. */
1189 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
1190 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
1191 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
1192 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
1194 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
1195 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
1196 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
1199 static int service_load(Unit
*u
) {
1201 Service
*s
= SERVICE(u
);
1205 /* Load a .service file */
1206 if ((r
= unit_load_fragment(u
)) < 0)
1209 #ifdef HAVE_SYSV_COMPAT
1210 /* Load a classic init script as a fallback, if we couldn't find anything */
1211 if (u
->load_state
== UNIT_STUB
)
1212 if ((r
= service_load_sysv(s
)) < 0)
1216 /* Still nothing found? Then let's give up */
1217 if (u
->load_state
== UNIT_STUB
)
1220 /* We were able to load something, then let's add in the
1221 * dropin directories. */
1222 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
1225 /* This is a new unit? Then let's add in some extras */
1226 if (u
->load_state
== UNIT_LOADED
) {
1227 if (s
->type
== _SERVICE_TYPE_INVALID
)
1228 s
->type
= s
->bus_name
? SERVICE_DBUS
: SERVICE_SIMPLE
;
1230 /* Oneshot services have disabled start timeout by default */
1231 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
1232 s
->timeout_start_usec
= 0;
1234 service_fix_output(s
);
1236 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
1239 if ((r
= unit_add_default_cgroups(u
)) < 0)
1242 #ifdef HAVE_SYSV_COMPAT
1243 if ((r
= sysv_fix_order(s
)) < 0)
1247 if ((r
= fsck_fix_order(s
)) < 0)
1251 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
1254 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
1255 s
->notify_access
= NOTIFY_MAIN
;
1257 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
1258 s
->notify_access
= NOTIFY_MAIN
;
1260 if (s
->type
== SERVICE_DBUS
|| s
->bus_name
)
1261 if ((r
= unit_add_two_dependencies_by_name(u
, UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, NULL
, true)) < 0)
1264 if (UNIT(s
)->default_dependencies
)
1265 if ((r
= service_add_default_dependencies(s
)) < 0)
1268 r
= unit_exec_context_defaults(u
, &s
->exec_context
);
1273 return service_verify(s
);
1276 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
1278 ServiceExecCommand c
;
1279 Service
*s
= SERVICE(u
);
1280 const char *prefix2
;
1285 p2
= strappend(prefix
, "\t");
1286 prefix2
= p2
? p2
: prefix
;
1289 "%sService State: %s\n"
1291 "%sReload Result: %s\n"
1292 "%sPermissionsStartOnly: %s\n"
1293 "%sRootDirectoryStartOnly: %s\n"
1294 "%sRemainAfterExit: %s\n"
1295 "%sGuessMainPID: %s\n"
1298 "%sNotifyAccess: %s\n",
1299 prefix
, service_state_to_string(s
->state
),
1300 prefix
, service_result_to_string(s
->result
),
1301 prefix
, service_result_to_string(s
->reload_result
),
1302 prefix
, yes_no(s
->permissions_start_only
),
1303 prefix
, yes_no(s
->root_directory_start_only
),
1304 prefix
, yes_no(s
->remain_after_exit
),
1305 prefix
, yes_no(s
->guess_main_pid
),
1306 prefix
, service_type_to_string(s
->type
),
1307 prefix
, service_restart_to_string(s
->restart
),
1308 prefix
, notify_access_to_string(s
->notify_access
));
1310 if (s
->control_pid
> 0)
1312 "%sControl PID: %lu\n",
1313 prefix
, (unsigned long) s
->control_pid
);
1315 if (s
->main_pid
> 0)
1318 "%sMain PID Known: %s\n"
1319 "%sMain PID Alien: %s\n",
1320 prefix
, (unsigned long) s
->main_pid
,
1321 prefix
, yes_no(s
->main_pid_known
),
1322 prefix
, yes_no(s
->main_pid_alien
));
1327 prefix
, s
->pid_file
);
1332 "%sBus Name Good: %s\n",
1333 prefix
, s
->bus_name
,
1334 prefix
, yes_no(s
->bus_name_good
));
1336 kill_context_dump(&s
->kill_context
, f
, prefix
);
1337 exec_context_dump(&s
->exec_context
, f
, prefix
);
1339 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
1341 if (!s
->exec_command
[c
])
1344 fprintf(f
, "%s-> %s:\n",
1345 prefix
, service_exec_command_to_string(c
));
1347 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
1350 #ifdef HAVE_SYSV_COMPAT
1353 "%sSysV Init Script has LSB Header: %s\n"
1354 "%sSysVEnabled: %s\n",
1355 prefix
, yes_no(s
->sysv_has_lsb
),
1356 prefix
, yes_no(s
->sysv_enabled
));
1358 if (s
->sysv_start_priority
>= 0)
1360 "%sSysVStartPriority: %i\n",
1361 prefix
, s
->sysv_start_priority
);
1363 if (s
->sysv_runlevels
)
1364 fprintf(f
, "%sSysVRunLevels: %s\n",
1365 prefix
, s
->sysv_runlevels
);
1368 if (s
->fsck_passno
> 0)
1370 "%sFsckPassNo: %i\n",
1371 prefix
, s
->fsck_passno
);
1374 fprintf(f
, "%sStatus Text: %s\n",
1375 prefix
, s
->status_text
);
1380 static int service_load_pid_file(Service
*s
, bool may_warn
) {
1390 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0) {
1392 log_info("PID file %s not readable (yet?) after %s.",
1393 s
->pid_file
, service_state_to_string(s
->state
));
1397 r
= parse_pid(k
, &pid
);
1403 if (kill(pid
, 0) < 0 && errno
!= EPERM
) {
1405 log_info("PID %lu read from file %s does not exist.",
1406 (unsigned long) pid
, s
->pid_file
);
1410 if (s
->main_pid_known
) {
1411 if (pid
== s
->main_pid
)
1414 log_debug("Main PID changing: %lu -> %lu",
1415 (unsigned long) s
->main_pid
, (unsigned long) pid
);
1416 service_unwatch_main_pid(s
);
1417 s
->main_pid_known
= false;
1419 log_debug("Main PID loaded: %lu", (unsigned long) pid
);
1421 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1424 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1425 /* FIXME: we need to do something here */
1431 static int service_search_main_pid(Service
*s
) {
1437 /* If we know it anyway, don't ever fallback to unreliable
1439 if (s
->main_pid_known
)
1442 if (!s
->guess_main_pid
)
1445 assert(s
->main_pid
<= 0);
1447 if ((pid
= cgroup_bonding_search_main_pid_list(UNIT(s
)->cgroup_bondings
)) <= 0)
1450 log_debug("Main PID guessed: %lu", (unsigned long) pid
);
1451 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1454 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1455 /* FIXME: we need to do something here */
1461 static void service_notify_sockets_dead(Service
*s
, bool failed_permanent
) {
1467 /* Notifies all our sockets when we die */
1469 if (s
->socket_fd
>= 0)
1472 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
)
1473 if (u
->type
== UNIT_SOCKET
)
1474 socket_notify_service_dead(SOCKET(u
), failed_permanent
);
1479 static void service_set_state(Service
*s
, ServiceState state
) {
1480 ServiceState old_state
;
1481 const UnitActiveState
*table
;
1484 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
1486 old_state
= s
->state
;
1489 service_unwatch_pid_file(s
);
1491 if (state
!= SERVICE_START_PRE
&&
1492 state
!= SERVICE_START
&&
1493 state
!= SERVICE_START_POST
&&
1494 state
!= SERVICE_RELOAD
&&
1495 state
!= SERVICE_STOP
&&
1496 state
!= SERVICE_STOP_SIGTERM
&&
1497 state
!= SERVICE_STOP_SIGKILL
&&
1498 state
!= SERVICE_STOP_POST
&&
1499 state
!= SERVICE_FINAL_SIGTERM
&&
1500 state
!= SERVICE_FINAL_SIGKILL
&&
1501 state
!= SERVICE_AUTO_RESTART
)
1502 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1504 if (state
!= SERVICE_START
&&
1505 state
!= SERVICE_START_POST
&&
1506 state
!= SERVICE_RUNNING
&&
1507 state
!= SERVICE_RELOAD
&&
1508 state
!= SERVICE_STOP
&&
1509 state
!= SERVICE_STOP_SIGTERM
&&
1510 state
!= SERVICE_STOP_SIGKILL
) {
1511 service_unwatch_main_pid(s
);
1512 s
->main_command
= NULL
;
1515 if (state
!= SERVICE_START_PRE
&&
1516 state
!= SERVICE_START
&&
1517 state
!= SERVICE_START_POST
&&
1518 state
!= SERVICE_RELOAD
&&
1519 state
!= SERVICE_STOP
&&
1520 state
!= SERVICE_STOP_SIGTERM
&&
1521 state
!= SERVICE_STOP_SIGKILL
&&
1522 state
!= SERVICE_STOP_POST
&&
1523 state
!= SERVICE_FINAL_SIGTERM
&&
1524 state
!= SERVICE_FINAL_SIGKILL
) {
1525 service_unwatch_control_pid(s
);
1526 s
->control_command
= NULL
;
1527 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1530 if (state
== SERVICE_DEAD
||
1531 state
== SERVICE_STOP
||
1532 state
== SERVICE_STOP_SIGTERM
||
1533 state
== SERVICE_STOP_SIGKILL
||
1534 state
== SERVICE_STOP_POST
||
1535 state
== SERVICE_FINAL_SIGTERM
||
1536 state
== SERVICE_FINAL_SIGKILL
||
1537 state
== SERVICE_FAILED
||
1538 state
== SERVICE_AUTO_RESTART
)
1539 service_notify_sockets_dead(s
, false);
1541 if (state
!= SERVICE_START_PRE
&&
1542 state
!= SERVICE_START
&&
1543 state
!= SERVICE_START_POST
&&
1544 state
!= SERVICE_RUNNING
&&
1545 state
!= SERVICE_RELOAD
&&
1546 state
!= SERVICE_STOP
&&
1547 state
!= SERVICE_STOP_SIGTERM
&&
1548 state
!= SERVICE_STOP_SIGKILL
&&
1549 state
!= SERVICE_STOP_POST
&&
1550 state
!= SERVICE_FINAL_SIGTERM
&&
1551 state
!= SERVICE_FINAL_SIGKILL
&&
1552 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
1553 service_close_socket_fd(s
);
1554 service_connection_unref(s
);
1557 if (state
== SERVICE_STOP
)
1558 service_stop_watchdog(s
);
1560 /* For the inactive states unit_notify() will trim the cgroup,
1561 * but for exit we have to do that ourselves... */
1562 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
1563 cgroup_bonding_trim_list(UNIT(s
)->cgroup_bondings
, true);
1565 if (old_state
!= state
)
1566 log_struct(LOG_DEBUG
,
1567 "UNIT=%s", UNIT(s
)->id
,
1568 "MESSAGE=%s changed %s -> %s", UNIT(s
)->id
,
1569 service_state_to_string(old_state
),
1570 service_state_to_string(state
),
1573 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
1574 s
->reload_result
= SERVICE_SUCCESS
;
1577 static int service_coldplug(Unit
*u
) {
1578 Service
*s
= SERVICE(u
);
1582 assert(s
->state
== SERVICE_DEAD
);
1584 if (s
->deserialized_state
!= s
->state
) {
1586 if (s
->deserialized_state
== SERVICE_START_PRE
||
1587 s
->deserialized_state
== SERVICE_START
||
1588 s
->deserialized_state
== SERVICE_START_POST
||
1589 s
->deserialized_state
== SERVICE_RELOAD
||
1590 s
->deserialized_state
== SERVICE_STOP
||
1591 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1592 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1593 s
->deserialized_state
== SERVICE_STOP_POST
||
1594 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1595 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
||
1596 s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
1597 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
|| s
->timeout_start_usec
> 0) {
1600 k
= s
->deserialized_state
== SERVICE_AUTO_RESTART
? s
->restart_usec
: s
->timeout_start_usec
;
1602 r
= unit_watch_timer(UNIT(s
), CLOCK_MONOTONIC
, true, k
, &s
->timer_watch
);
1608 if ((s
->deserialized_state
== SERVICE_START
&&
1609 (s
->type
== SERVICE_FORKING
||
1610 s
->type
== SERVICE_DBUS
||
1611 s
->type
== SERVICE_ONESHOT
||
1612 s
->type
== SERVICE_NOTIFY
)) ||
1613 s
->deserialized_state
== SERVICE_START_POST
||
1614 s
->deserialized_state
== SERVICE_RUNNING
||
1615 s
->deserialized_state
== SERVICE_RELOAD
||
1616 s
->deserialized_state
== SERVICE_STOP
||
1617 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1618 s
->deserialized_state
== SERVICE_STOP_SIGKILL
)
1619 if (s
->main_pid
> 0)
1620 if ((r
= unit_watch_pid(UNIT(s
), s
->main_pid
)) < 0)
1623 if (s
->deserialized_state
== SERVICE_START_PRE
||
1624 s
->deserialized_state
== SERVICE_START
||
1625 s
->deserialized_state
== SERVICE_START_POST
||
1626 s
->deserialized_state
== SERVICE_RELOAD
||
1627 s
->deserialized_state
== SERVICE_STOP
||
1628 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1629 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1630 s
->deserialized_state
== SERVICE_STOP_POST
||
1631 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1632 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
)
1633 if (s
->control_pid
> 0)
1634 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1637 if (s
->deserialized_state
== SERVICE_START_POST
||
1638 s
->deserialized_state
== SERVICE_RUNNING
)
1639 service_handle_watchdog(s
);
1641 service_set_state(s
, s
->deserialized_state
);
1646 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1650 unsigned rn_fds
= 0;
1657 if (s
->socket_fd
>= 0)
1660 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1665 if (u
->type
!= UNIT_SOCKET
)
1670 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1682 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1688 memcpy(t
, rfds
, rn_fds
* sizeof(int));
1689 memcpy(t
+rn_fds
, cfds
, cn_fds
* sizeof(int));
1694 rn_fds
= rn_fds
+cn_fds
;
1709 static int service_spawn(
1714 bool apply_permissions
,
1716 bool apply_tty_stdin
,
1717 bool set_notify_socket
,
1723 int *fds
= NULL
, *fdsbuf
= NULL
;
1724 unsigned n_fds
= 0, n_env
= 0;
1725 char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
;
1732 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1733 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1734 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1736 if (s
->socket_fd
>= 0) {
1737 fds
= &s
->socket_fd
;
1740 if ((r
= service_collect_fds(s
, &fdsbuf
, &n_fds
)) < 0)
1747 if (timeout
&& s
->timeout_start_usec
) {
1748 r
= unit_watch_timer(UNIT(s
), CLOCK_MONOTONIC
, true, s
->timeout_start_usec
, &s
->timer_watch
);
1752 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1754 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1759 our_env
= new0(char*, 5);
1765 if (set_notify_socket
)
1766 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1771 if (s
->main_pid
> 0)
1772 if (asprintf(our_env
+ n_env
++, "MAINPID=%lu", (unsigned long) s
->main_pid
) < 0) {
1777 if (s
->watchdog_usec
> 0)
1778 if (asprintf(our_env
+ n_env
++, "WATCHDOG_USEC=%llu", (unsigned long long) s
->watchdog_usec
) < 0) {
1783 if (s
->meta
.manager
->running_as
!= SYSTEMD_SYSTEM
)
1784 if (asprintf(our_env
+ n_env
++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1789 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1803 UNIT(s
)->manager
->confirm_spawn
,
1804 UNIT(s
)->cgroup_bondings
,
1805 UNIT(s
)->cgroup_attributes
,
1806 is_control
? "control" : NULL
,
1808 s
->type
== SERVICE_IDLE
? UNIT(s
)->manager
->idle_pipe
: NULL
,
1814 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1815 /* FIXME: we need to do something here */
1821 strv_free(final_env
);
1831 strv_free(final_env
);
1834 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1839 static int main_pid_good(Service
*s
) {
1842 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1845 /* If we know the pid file, then lets just check if it is
1847 if (s
->main_pid_known
) {
1849 /* If it's an alien child let's check if it is still
1851 if (s
->main_pid_alien
)
1852 return kill(s
->main_pid
, 0) >= 0 || errno
!= ESRCH
;
1854 /* .. otherwise assume we'll get a SIGCHLD for it,
1855 * which we really should wait for to collect exit
1856 * status and code */
1857 return s
->main_pid
> 0;
1860 /* We don't know the pid */
1864 static int control_pid_good(Service
*s
) {
1867 return s
->control_pid
> 0;
1870 static int cgroup_good(Service
*s
) {
1875 if ((r
= cgroup_bonding_is_empty_list(UNIT(s
)->cgroup_bondings
)) < 0)
1881 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1885 if (f
!= SERVICE_SUCCESS
)
1888 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1890 if (allow_restart
&&
1891 !s
->forbid_restart
&&
1892 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1893 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& s
->result
== SERVICE_SUCCESS
) ||
1894 (s
->restart
== SERVICE_RESTART_ON_FAILURE
&& s
->result
!= SERVICE_SUCCESS
) ||
1895 (s
->restart
== SERVICE_RESTART_ON_ABORT
&& (s
->result
== SERVICE_FAILURE_SIGNAL
||
1896 s
->result
== SERVICE_FAILURE_CORE_DUMP
))) &&
1897 (s
->result
!= SERVICE_FAILURE_EXIT_CODE
||
1898 !set_contains(s
->restart_ignore_status
.code
, INT_TO_PTR(s
->main_exec_status
.status
))) &&
1899 (s
->result
!= SERVICE_FAILURE_SIGNAL
||
1900 !set_contains(s
->restart_ignore_status
.signal
, INT_TO_PTR(s
->main_exec_status
.status
)))
1903 r
= unit_watch_timer(UNIT(s
), CLOCK_MONOTONIC
, true, s
->restart_usec
, &s
->timer_watch
);
1907 service_set_state(s
, SERVICE_AUTO_RESTART
);
1910 s
->forbid_restart
= false;
1915 log_warning("%s failed to run install restart timer: %s", UNIT(s
)->id
, strerror(-r
));
1916 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1919 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
1921 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1925 if (f
!= SERVICE_SUCCESS
)
1928 service_unwatch_control_pid(s
);
1930 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1931 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1933 r
= service_spawn(s
,
1937 !s
->permissions_start_only
,
1938 !s
->root_directory_start_only
,
1947 service_set_state(s
, SERVICE_STOP_POST
);
1949 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1954 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s
)->id
, strerror(-r
));
1955 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1958 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1960 Set
*pid_set
= NULL
;
1961 bool wait_for_exit
= false;
1965 if (f
!= SERVICE_SUCCESS
)
1968 if (s
->kill_context
.kill_mode
!= KILL_NONE
) {
1969 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? s
->kill_context
.kill_signal
: SIGKILL
;
1971 if (s
->main_pid
> 0) {
1972 if (kill_and_sigcont(s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1973 log_warning("Failed to kill main process %li: %m", (long) s
->main_pid
);
1975 wait_for_exit
= !s
->main_pid_alien
;
1978 if (s
->control_pid
> 0) {
1979 if (kill_and_sigcont(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1980 log_warning("Failed to kill control process %li: %m", (long) s
->control_pid
);
1982 wait_for_exit
= true;
1985 if (s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
) {
1987 pid_set
= set_new(trivial_hash_func
, trivial_compare_func
);
1993 /* Exclude the main/control pids from being killed via the cgroup */
1994 if (s
->main_pid
> 0)
1995 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->main_pid
))) < 0)
1998 if (s
->control_pid
> 0)
1999 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0)
2002 r
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, sig
, true, false, pid_set
, NULL
);
2004 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
2005 log_warning("Failed to kill control group: %s", strerror(-r
));
2007 wait_for_exit
= true;
2014 if (wait_for_exit
) {
2015 if (s
->timeout_stop_usec
> 0) {
2016 r
= unit_watch_timer(UNIT(s
), CLOCK_MONOTONIC
, true, s
->timeout_stop_usec
, &s
->timer_watch
);
2021 service_set_state(s
, state
);
2022 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
2023 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2025 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2030 log_warning("%s failed to kill processes: %s", UNIT(s
)->id
, strerror(-r
));
2032 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
2033 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
2035 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2041 static void service_enter_stop(Service
*s
, ServiceResult f
) {
2046 if (f
!= SERVICE_SUCCESS
)
2049 service_unwatch_control_pid(s
);
2051 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
2052 s
->control_command_id
= SERVICE_EXEC_STOP
;
2054 r
= service_spawn(s
,
2058 !s
->permissions_start_only
,
2059 !s
->root_directory_start_only
,
2067 service_set_state(s
, SERVICE_STOP
);
2069 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2074 log_warning("%s failed to run 'stop' task: %s", UNIT(s
)->id
, strerror(-r
));
2075 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2078 static void service_enter_running(Service
*s
, ServiceResult f
) {
2079 int main_pid_ok
, cgroup_ok
;
2082 if (f
!= SERVICE_SUCCESS
)
2085 main_pid_ok
= main_pid_good(s
);
2086 cgroup_ok
= cgroup_good(s
);
2088 if ((main_pid_ok
> 0 || (main_pid_ok
< 0 && cgroup_ok
!= 0)) &&
2089 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
2090 service_set_state(s
, SERVICE_RUNNING
);
2091 else if (s
->remain_after_exit
)
2092 service_set_state(s
, SERVICE_EXITED
);
2094 service_enter_stop(s
, SERVICE_SUCCESS
);
2097 static void service_enter_start_post(Service
*s
) {
2101 service_unwatch_control_pid(s
);
2103 if (s
->watchdog_usec
> 0)
2104 service_reset_watchdog(s
);
2106 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
2107 s
->control_command_id
= SERVICE_EXEC_START_POST
;
2109 r
= service_spawn(s
,
2113 !s
->permissions_start_only
,
2114 !s
->root_directory_start_only
,
2122 service_set_state(s
, SERVICE_START_POST
);
2124 service_enter_running(s
, SERVICE_SUCCESS
);
2129 log_warning("%s failed to run 'start-post' task: %s", UNIT(s
)->id
, strerror(-r
));
2130 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2133 static void service_enter_start(Service
*s
) {
2140 assert(s
->exec_command
[SERVICE_EXEC_START
]);
2141 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
|| s
->type
== SERVICE_ONESHOT
);
2143 if (s
->type
== SERVICE_FORKING
)
2144 service_unwatch_control_pid(s
);
2146 service_unwatch_main_pid(s
);
2148 /* We want to ensure that nobody leaks processes from
2149 * START_PRE here, so let's go on a killing spree, People
2150 * should not spawn long running processes from START_PRE. */
2151 cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, SIGKILL
, true, true, NULL
, "control");
2153 if (s
->type
== SERVICE_FORKING
) {
2154 s
->control_command_id
= SERVICE_EXEC_START
;
2155 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
2157 s
->main_command
= NULL
;
2159 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2160 s
->control_command
= NULL
;
2162 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
2165 r
= service_spawn(s
,
2167 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
|| s
->type
== SERVICE_ONESHOT
,
2172 s
->notify_access
!= NOTIFY_NONE
,
2178 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_IDLE
) {
2179 /* For simple services we immediately start
2180 * the START_POST binaries. */
2182 service_set_main_pid(s
, pid
);
2183 service_enter_start_post(s
);
2185 } else if (s
->type
== SERVICE_FORKING
) {
2187 /* For forking services we wait until the start
2188 * process exited. */
2190 s
->control_pid
= pid
;
2191 service_set_state(s
, SERVICE_START
);
2193 } else if (s
->type
== SERVICE_ONESHOT
||
2194 s
->type
== SERVICE_DBUS
||
2195 s
->type
== SERVICE_NOTIFY
) {
2197 /* For oneshot services we wait until the start
2198 * process exited, too, but it is our main process. */
2200 /* For D-Bus services we know the main pid right away,
2201 * but wait for the bus name to appear on the
2202 * bus. Notify services are similar. */
2204 service_set_main_pid(s
, pid
);
2205 service_set_state(s
, SERVICE_START
);
2207 assert_not_reached("Unknown service type");
2212 log_warning("%s failed to run 'start' task: %s", UNIT(s
)->id
, strerror(-r
));
2213 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2216 static void service_enter_start_pre(Service
*s
) {
2221 service_unwatch_control_pid(s
);
2223 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
2225 /* Before we start anything, let's clear up what might
2226 * be left from previous runs. */
2227 cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, SIGKILL
, true, true, NULL
, "control");
2229 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
2231 r
= service_spawn(s
,
2235 !s
->permissions_start_only
,
2236 !s
->root_directory_start_only
,
2244 service_set_state(s
, SERVICE_START_PRE
);
2246 service_enter_start(s
);
2251 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s
)->id
, strerror(-r
));
2252 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2255 static void service_enter_restart(Service
*s
) {
2260 dbus_error_init(&error
);
2262 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
2263 /* Don't restart things if we are going down anyway */
2264 log_info("Stop job pending for unit, delaying automatic restart.");
2266 r
= unit_watch_timer(UNIT(s
), CLOCK_MONOTONIC
, true, s
->restart_usec
, &s
->timer_watch
);
2273 /* Any units that are bound to this service must also be
2274 * restarted. We use JOB_RESTART (instead of the more obvious
2275 * JOB_START) here so that those dependency jobs will be added
2277 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
);
2281 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2282 * it will be canceled as part of the service_stop() call that
2283 * is executed as part of JOB_RESTART. */
2285 log_debug("%s scheduled restart job.", UNIT(s
)->id
);
2289 log_warning("%s failed to schedule restart job: %s", UNIT(s
)->id
, bus_error(&error
, -r
));
2290 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
2292 dbus_error_free(&error
);
2295 static void service_enter_reload(Service
*s
) {
2300 service_unwatch_control_pid(s
);
2302 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
2303 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
2305 r
= service_spawn(s
,
2309 !s
->permissions_start_only
,
2310 !s
->root_directory_start_only
,
2318 service_set_state(s
, SERVICE_RELOAD
);
2320 service_enter_running(s
, SERVICE_SUCCESS
);
2325 log_warning("%s failed to run 'reload' task: %s", UNIT(s
)->id
, strerror(-r
));
2326 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2327 service_enter_running(s
, SERVICE_SUCCESS
);
2330 static void service_run_next_control(Service
*s
) {
2334 assert(s
->control_command
);
2335 assert(s
->control_command
->command_next
);
2337 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
2339 s
->control_command
= s
->control_command
->command_next
;
2340 service_unwatch_control_pid(s
);
2342 r
= service_spawn(s
,
2346 !s
->permissions_start_only
,
2347 !s
->root_directory_start_only
,
2348 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
2349 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
2359 log_warning("%s failed to run next control task: %s", UNIT(s
)->id
, strerror(-r
));
2361 if (s
->state
== SERVICE_START_PRE
)
2362 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2363 else if (s
->state
== SERVICE_STOP
)
2364 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2365 else if (s
->state
== SERVICE_STOP_POST
)
2366 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2367 else if (s
->state
== SERVICE_RELOAD
) {
2368 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2369 service_enter_running(s
, SERVICE_SUCCESS
);
2371 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2374 static void service_run_next_main(Service
*s
) {
2379 assert(s
->main_command
);
2380 assert(s
->main_command
->command_next
);
2381 assert(s
->type
== SERVICE_ONESHOT
);
2383 s
->main_command
= s
->main_command
->command_next
;
2384 service_unwatch_main_pid(s
);
2386 r
= service_spawn(s
,
2393 s
->notify_access
!= NOTIFY_NONE
,
2399 service_set_main_pid(s
, pid
);
2404 log_warning("%s failed to run next main task: %s", UNIT(s
)->id
, strerror(-r
));
2405 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2408 static int service_start_limit_test(Service
*s
) {
2411 if (ratelimit_test(&s
->start_limit
))
2414 switch (s
->start_limit_action
) {
2416 case SERVICE_START_LIMIT_NONE
:
2417 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s
)->id
);
2420 case SERVICE_START_LIMIT_REBOOT
: {
2424 dbus_error_init(&error
);
2426 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s
)->id
);
2428 r
= manager_add_job_by_name(UNIT(s
)->manager
, JOB_START
, SPECIAL_REBOOT_TARGET
, JOB_REPLACE
, true, &error
, NULL
);
2430 log_error("Failed to reboot: %s.", bus_error(&error
, r
));
2431 dbus_error_free(&error
);
2437 case SERVICE_START_LIMIT_REBOOT_FORCE
:
2438 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s
)->id
);
2439 UNIT(s
)->manager
->exit_code
= MANAGER_REBOOT
;
2442 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE
:
2443 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s
)->id
);
2445 reboot(RB_AUTOBOOT
);
2449 log_error("start limit action=%i", s
->start_limit_action
);
2450 assert_not_reached("Unknown StartLimitAction.");
2456 static int service_start(Unit
*u
) {
2457 Service
*s
= SERVICE(u
);
2462 /* We cannot fulfill this request right now, try again later
2464 if (s
->state
== SERVICE_STOP
||
2465 s
->state
== SERVICE_STOP_SIGTERM
||
2466 s
->state
== SERVICE_STOP_SIGKILL
||
2467 s
->state
== SERVICE_STOP_POST
||
2468 s
->state
== SERVICE_FINAL_SIGTERM
||
2469 s
->state
== SERVICE_FINAL_SIGKILL
)
2472 /* Already on it! */
2473 if (s
->state
== SERVICE_START_PRE
||
2474 s
->state
== SERVICE_START
||
2475 s
->state
== SERVICE_START_POST
)
2478 /* A service that will be restarted must be stopped first to
2479 * trigger BindsTo and/or OnFailure dependencies. If a user
2480 * does not want to wait for the holdoff time to elapse, the
2481 * service should be manually restarted, not started. We
2482 * simply return EAGAIN here, so that any start jobs stay
2483 * queued, and assume that the auto restart timer will
2484 * eventually trigger the restart. */
2485 if (s
->state
== SERVICE_AUTO_RESTART
)
2488 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_FAILED
);
2490 /* Make sure we don't enter a busy loop of some kind. */
2491 r
= service_start_limit_test(s
);
2493 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT
, false);
2497 s
->result
= SERVICE_SUCCESS
;
2498 s
->reload_result
= SERVICE_SUCCESS
;
2499 s
->main_pid_known
= false;
2500 s
->main_pid_alien
= false;
2501 s
->forbid_restart
= false;
2503 service_enter_start_pre(s
);
2507 static int service_stop(Unit
*u
) {
2508 Service
*s
= SERVICE(u
);
2512 /* Don't create restart jobs from here. */
2513 s
->forbid_restart
= true;
2516 if (s
->state
== SERVICE_STOP
||
2517 s
->state
== SERVICE_STOP_SIGTERM
||
2518 s
->state
== SERVICE_STOP_SIGKILL
||
2519 s
->state
== SERVICE_STOP_POST
||
2520 s
->state
== SERVICE_FINAL_SIGTERM
||
2521 s
->state
== SERVICE_FINAL_SIGKILL
)
2524 /* A restart will be scheduled or is in progress. */
2525 if (s
->state
== SERVICE_AUTO_RESTART
) {
2526 service_set_state(s
, SERVICE_DEAD
);
2530 /* If there's already something running we go directly into
2532 if (s
->state
== SERVICE_START_PRE
||
2533 s
->state
== SERVICE_START
||
2534 s
->state
== SERVICE_START_POST
||
2535 s
->state
== SERVICE_RELOAD
) {
2536 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2540 assert(s
->state
== SERVICE_RUNNING
||
2541 s
->state
== SERVICE_EXITED
);
2543 service_enter_stop(s
, SERVICE_SUCCESS
);
2547 static int service_reload(Unit
*u
) {
2548 Service
*s
= SERVICE(u
);
2552 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2554 service_enter_reload(s
);
2558 static bool service_can_reload(Unit
*u
) {
2559 Service
*s
= SERVICE(u
);
2563 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2566 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2567 Service
*s
= SERVICE(u
);
2573 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2574 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2575 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2577 if (s
->control_pid
> 0)
2578 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
2580 if (s
->main_pid_known
&& s
->main_pid
> 0)
2581 unit_serialize_item_format(u
, f
, "main-pid", "%lu", (unsigned long) s
->main_pid
);
2583 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2586 unit_serialize_item(u
, f
, "status-text", s
->status_text
);
2588 /* FIXME: There's a minor uncleanliness here: if there are
2589 * multiple commands attached here, we will start from the
2590 * first one again */
2591 if (s
->control_command_id
>= 0)
2592 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2594 if (s
->socket_fd
>= 0) {
2597 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
2600 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
2603 if (s
->main_exec_status
.pid
> 0) {
2604 unit_serialize_item_format(u
, f
, "main-exec-status-pid", "%lu", (unsigned long) s
->main_exec_status
.pid
);
2605 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2606 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2608 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2609 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2610 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2613 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
2614 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2619 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2620 Service
*s
= SERVICE(u
);
2627 if (streq(key
, "state")) {
2630 if ((state
= service_state_from_string(value
)) < 0)
2631 log_debug("Failed to parse state value %s", value
);
2633 s
->deserialized_state
= state
;
2634 } else if (streq(key
, "result")) {
2637 f
= service_result_from_string(value
);
2639 log_debug("Failed to parse result value %s", value
);
2640 else if (f
!= SERVICE_SUCCESS
)
2643 } else if (streq(key
, "reload-result")) {
2646 f
= service_result_from_string(value
);
2648 log_debug("Failed to parse reload result value %s", value
);
2649 else if (f
!= SERVICE_SUCCESS
)
2650 s
->reload_result
= f
;
2652 } else if (streq(key
, "control-pid")) {
2655 if (parse_pid(value
, &pid
) < 0)
2656 log_debug("Failed to parse control-pid value %s", value
);
2658 s
->control_pid
= pid
;
2659 } else if (streq(key
, "main-pid")) {
2662 if (parse_pid(value
, &pid
) < 0)
2663 log_debug("Failed to parse main-pid value %s", value
);
2665 service_set_main_pid(s
, (pid_t
) pid
);
2666 } else if (streq(key
, "main-pid-known")) {
2669 if ((b
= parse_boolean(value
)) < 0)
2670 log_debug("Failed to parse main-pid-known value %s", value
);
2672 s
->main_pid_known
= b
;
2673 } else if (streq(key
, "status-text")) {
2676 if ((t
= strdup(value
))) {
2677 free(s
->status_text
);
2681 } else if (streq(key
, "control-command")) {
2682 ServiceExecCommand id
;
2684 if ((id
= service_exec_command_from_string(value
)) < 0)
2685 log_debug("Failed to parse exec-command value %s", value
);
2687 s
->control_command_id
= id
;
2688 s
->control_command
= s
->exec_command
[id
];
2690 } else if (streq(key
, "socket-fd")) {
2693 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2694 log_debug("Failed to parse socket-fd value %s", value
);
2697 if (s
->socket_fd
>= 0)
2698 close_nointr_nofail(s
->socket_fd
);
2699 s
->socket_fd
= fdset_remove(fds
, fd
);
2701 } else if (streq(key
, "main-exec-status-pid")) {
2704 if (parse_pid(value
, &pid
) < 0)
2705 log_debug("Failed to parse main-exec-status-pid value %s", value
);
2707 s
->main_exec_status
.pid
= pid
;
2708 } else if (streq(key
, "main-exec-status-code")) {
2711 if (safe_atoi(value
, &i
) < 0)
2712 log_debug("Failed to parse main-exec-status-code value %s", value
);
2714 s
->main_exec_status
.code
= i
;
2715 } else if (streq(key
, "main-exec-status-status")) {
2718 if (safe_atoi(value
, &i
) < 0)
2719 log_debug("Failed to parse main-exec-status-status value %s", value
);
2721 s
->main_exec_status
.status
= i
;
2722 } else if (streq(key
, "main-exec-status-start"))
2723 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2724 else if (streq(key
, "main-exec-status-exit"))
2725 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2726 else if (streq(key
, "watchdog-timestamp"))
2727 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2729 log_debug("Unknown serialization key '%s'", key
);
2734 static UnitActiveState
service_active_state(Unit
*u
) {
2735 const UnitActiveState
*table
;
2739 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2741 return table
[SERVICE(u
)->state
];
2744 static const char *service_sub_state_to_string(Unit
*u
) {
2747 return service_state_to_string(SERVICE(u
)->state
);
2750 static bool service_check_gc(Unit
*u
) {
2751 Service
*s
= SERVICE(u
);
2755 /* Never clean up services that still have a process around,
2756 * even if the service is formally dead. */
2757 if (cgroup_good(s
) > 0 ||
2758 main_pid_good(s
) > 0 ||
2759 control_pid_good(s
) > 0)
2762 #ifdef HAVE_SYSV_COMPAT
2770 static bool service_check_snapshot(Unit
*u
) {
2771 Service
*s
= SERVICE(u
);
2775 return !s
->got_socket_fd
;
2778 static int service_retry_pid_file(Service
*s
) {
2781 assert(s
->pid_file
);
2782 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2784 r
= service_load_pid_file(s
, false);
2788 service_unwatch_pid_file(s
);
2790 service_enter_running(s
, SERVICE_SUCCESS
);
2794 static int service_watch_pid_file(Service
*s
) {
2797 log_debug("Setting watch for %s's PID file %s", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
2798 r
= path_spec_watch(s
->pid_file_pathspec
, UNIT(s
));
2802 /* the pidfile might have appeared just before we set the watch */
2803 service_retry_pid_file(s
);
2807 log_error("Failed to set a watch for %s's PID file %s: %s",
2808 UNIT(s
)->id
, s
->pid_file_pathspec
->path
, strerror(-r
));
2809 service_unwatch_pid_file(s
);
2813 static int service_demand_pid_file(Service
*s
) {
2816 assert(s
->pid_file
);
2817 assert(!s
->pid_file_pathspec
);
2819 ps
= new0(PathSpec
, 1);
2823 ps
->path
= strdup(s
->pid_file
);
2829 path_kill_slashes(ps
->path
);
2831 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2832 * keep their PID file open all the time. */
2833 ps
->type
= PATH_MODIFIED
;
2834 ps
->inotify_fd
= -1;
2836 s
->pid_file_pathspec
= ps
;
2838 return service_watch_pid_file(s
);
2841 static void service_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
2842 Service
*s
= SERVICE(u
);
2846 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2847 assert(s
->pid_file_pathspec
);
2848 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2850 log_debug("inotify event for %s", u
->id
);
2852 if (path_spec_fd_event(s
->pid_file_pathspec
, events
) < 0)
2855 if (service_retry_pid_file(s
) == 0)
2858 if (service_watch_pid_file(s
) < 0)
2863 service_unwatch_pid_file(s
);
2864 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2867 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2868 Service
*s
= SERVICE(u
);
2874 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2875 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2876 f
= SERVICE_SUCCESS
;
2877 else if (code
== CLD_EXITED
)
2878 f
= SERVICE_FAILURE_EXIT_CODE
;
2879 else if (code
== CLD_KILLED
)
2880 f
= SERVICE_FAILURE_SIGNAL
;
2881 else if (code
== CLD_DUMPED
)
2882 f
= SERVICE_FAILURE_CORE_DUMP
;
2884 assert_not_reached("Unknown code");
2886 if (s
->main_pid
== pid
) {
2887 /* Forking services may occasionally move to a new PID.
2888 * As long as they update the PID file before exiting the old
2889 * PID, they're fine. */
2890 if (service_load_pid_file(s
, false) == 0)
2894 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2896 /* If this is not a forking service than the main
2897 * process got started and hence we copy the exit
2898 * status so that it is recorded both as main and as
2899 * control process exit status */
2900 if (s
->main_command
) {
2901 s
->main_command
->exec_status
= s
->main_exec_status
;
2903 if (s
->main_command
->ignore
)
2904 f
= SERVICE_SUCCESS
;
2907 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2908 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2909 u
->id
, sigchld_code_to_string(code
), status
,
2910 strna(code
== CLD_EXITED
2911 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2912 : signal_to_string(status
)),
2914 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2915 "EXIT_STATUS=%i", status
,
2918 if (f
!= SERVICE_SUCCESS
)
2921 if (s
->main_command
&&
2922 s
->main_command
->command_next
&&
2923 f
== SERVICE_SUCCESS
) {
2925 /* There is another command to *
2926 * execute, so let's do that. */
2928 log_debug("%s running next main command for state %s", u
->id
, service_state_to_string(s
->state
));
2929 service_run_next_main(s
);
2933 /* The service exited, so the service is officially
2935 s
->main_command
= NULL
;
2939 case SERVICE_START_POST
:
2940 case SERVICE_RELOAD
:
2942 /* Need to wait until the operation is
2947 if (s
->type
== SERVICE_ONESHOT
) {
2948 /* This was our main goal, so let's go on */
2949 if (f
== SERVICE_SUCCESS
)
2950 service_enter_start_post(s
);
2952 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2958 case SERVICE_RUNNING
:
2959 service_enter_running(s
, f
);
2962 case SERVICE_STOP_SIGTERM
:
2963 case SERVICE_STOP_SIGKILL
:
2965 if (!control_pid_good(s
))
2966 service_enter_stop_post(s
, f
);
2968 /* If there is still a control process, wait for that first */
2972 assert_not_reached("Uh, main process died at wrong time.");
2976 } else if (s
->control_pid
== pid
) {
2980 if (s
->control_command
) {
2981 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2983 if (s
->control_command
->ignore
)
2984 f
= SERVICE_SUCCESS
;
2987 log_full(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2988 "%s: control process exited, code=%s status=%i", u
->id
, sigchld_code_to_string(code
), status
);
2990 if (f
!= SERVICE_SUCCESS
)
2993 /* Immediately get rid of the cgroup, so that the
2994 * kernel doesn't delay the cgroup empty messages for
2995 * the service cgroup any longer than necessary */
2996 cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, SIGKILL
, true, true, NULL
, "control");
2998 if (s
->control_command
&&
2999 s
->control_command
->command_next
&&
3000 f
== SERVICE_SUCCESS
) {
3002 /* There is another command to *
3003 * execute, so let's do that. */
3005 log_debug("%s running next control command for state %s", u
->id
, service_state_to_string(s
->state
));
3006 service_run_next_control(s
);
3009 /* No further commands for this step, so let's
3010 * figure out what to do next */
3012 s
->control_command
= NULL
;
3013 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
3015 log_debug("%s got final SIGCHLD for state %s", u
->id
, service_state_to_string(s
->state
));
3019 case SERVICE_START_PRE
:
3020 if (f
== SERVICE_SUCCESS
)
3021 service_enter_start(s
);
3023 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
3027 if (s
->type
!= SERVICE_FORKING
)
3028 /* Maybe spurious event due to a reload that changed the type? */
3031 if (f
!= SERVICE_SUCCESS
) {
3032 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
3037 bool has_start_post
;
3040 /* Let's try to load the pid file here if we can.
3041 * The PID file might actually be created by a START_POST
3042 * script. In that case don't worry if the loading fails. */
3044 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
3045 r
= service_load_pid_file(s
, !has_start_post
);
3046 if (!has_start_post
&& r
< 0) {
3047 r
= service_demand_pid_file(s
);
3048 if (r
< 0 || !cgroup_good(s
))
3049 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
3053 service_search_main_pid(s
);
3055 service_enter_start_post(s
);
3058 case SERVICE_START_POST
:
3059 if (f
!= SERVICE_SUCCESS
) {
3060 service_enter_stop(s
, f
);
3067 r
= service_load_pid_file(s
, true);
3069 r
= service_demand_pid_file(s
);
3070 if (r
< 0 || !cgroup_good(s
))
3071 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
3075 service_search_main_pid(s
);
3077 service_enter_running(s
, SERVICE_SUCCESS
);
3080 case SERVICE_RELOAD
:
3081 if (f
== SERVICE_SUCCESS
) {
3082 service_load_pid_file(s
, true);
3083 service_search_main_pid(s
);
3086 s
->reload_result
= f
;
3087 service_enter_running(s
, SERVICE_SUCCESS
);
3091 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3094 case SERVICE_STOP_SIGTERM
:
3095 case SERVICE_STOP_SIGKILL
:
3096 if (main_pid_good(s
) <= 0)
3097 service_enter_stop_post(s
, f
);
3099 /* If there is still a service
3100 * process around, wait until
3101 * that one quit, too */
3104 case SERVICE_STOP_POST
:
3105 case SERVICE_FINAL_SIGTERM
:
3106 case SERVICE_FINAL_SIGKILL
:
3107 service_enter_dead(s
, f
, true);
3111 assert_not_reached("Uh, control process died at wrong time.");
3116 /* Notify clients about changed exit status */
3117 unit_add_to_dbus_queue(u
);
3120 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
3121 Service
*s
= SERVICE(u
);
3124 assert(elapsed
== 1);
3126 if (w
== &s
->watchdog_watch
) {
3127 service_handle_watchdog(s
);
3131 assert(w
== &s
->timer_watch
);
3135 case SERVICE_START_PRE
:
3137 log_warning("%s operation timed out. Terminating.", u
->id
);
3138 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3141 case SERVICE_START_POST
:
3142 log_warning("%s operation timed out. Stopping.", u
->id
);
3143 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
3146 case SERVICE_RELOAD
:
3147 log_warning("%s operation timed out. Stopping.", u
->id
);
3148 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
3149 service_enter_running(s
, SERVICE_SUCCESS
);
3153 log_warning("%s stopping timed out. Terminating.", u
->id
);
3154 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3157 case SERVICE_STOP_SIGTERM
:
3158 if (s
->kill_context
.send_sigkill
) {
3159 log_warning("%s stopping timed out. Killing.", u
->id
);
3160 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
3162 log_warning("%s stopping timed out. Skipping SIGKILL.", u
->id
);
3163 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
3168 case SERVICE_STOP_SIGKILL
:
3169 /* Uh, we sent a SIGKILL and it is still not gone?
3170 * Must be something we cannot kill, so let's just be
3171 * weirded out and continue */
3173 log_warning("%s still around after SIGKILL. Ignoring.", u
->id
);
3174 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
3177 case SERVICE_STOP_POST
:
3178 log_warning("%s stopping timed out (2). Terminating.", u
->id
);
3179 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3182 case SERVICE_FINAL_SIGTERM
:
3183 if (s
->kill_context
.send_sigkill
) {
3184 log_warning("%s stopping timed out (2). Killing.", u
->id
);
3185 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
3187 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u
->id
);
3188 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
3193 case SERVICE_FINAL_SIGKILL
:
3194 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u
->id
);
3195 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
3198 case SERVICE_AUTO_RESTART
:
3199 log_info("%s holdoff time over, scheduling restart.", u
->id
);
3200 service_enter_restart(s
);
3204 assert_not_reached("Timeout at wrong time.");
3208 static void service_cgroup_notify_event(Unit
*u
) {
3209 Service
*s
= SERVICE(u
);
3213 log_debug("%s: cgroup is empty", u
->id
);
3217 /* Waiting for SIGCHLD is usually more interesting,
3218 * because it includes return codes/signals. Which is
3219 * why we ignore the cgroup events for most cases,
3220 * except when we don't know pid which to expect the
3224 case SERVICE_START_POST
:
3225 /* If we were hoping for the daemon to write its PID file,
3226 * we can give up now. */
3227 if (s
->pid_file_pathspec
) {
3228 log_warning("%s never wrote its PID file. Failing.", UNIT(s
)->id
);
3229 service_unwatch_pid_file(s
);
3230 if (s
->state
== SERVICE_START
)
3231 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
3233 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
3237 case SERVICE_RUNNING
:
3238 /* service_enter_running() will figure out what to do */
3239 service_enter_running(s
, SERVICE_SUCCESS
);
3242 case SERVICE_STOP_SIGTERM
:
3243 case SERVICE_STOP_SIGKILL
:
3245 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
3246 service_enter_stop_post(s
, SERVICE_SUCCESS
);
3250 case SERVICE_FINAL_SIGTERM
:
3251 case SERVICE_FINAL_SIGKILL
:
3252 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
3253 service_enter_dead(s
, SERVICE_SUCCESS
, true);
3262 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
) {
3263 Service
*s
= SERVICE(u
);
3268 if (s
->notify_access
== NOTIFY_NONE
) {
3269 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3270 u
->id
, (unsigned long) pid
);
3274 if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
3275 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3276 u
->id
, (unsigned long) pid
, (unsigned long) s
->main_pid
);
3280 log_debug("%s: Got message", u
->id
);
3282 /* Interpret MAINPID= */
3283 if ((e
= strv_find_prefix(tags
, "MAINPID=")) &&
3284 (s
->state
== SERVICE_START
||
3285 s
->state
== SERVICE_START_POST
||
3286 s
->state
== SERVICE_RUNNING
||
3287 s
->state
== SERVICE_RELOAD
)) {
3289 if (parse_pid(e
+ 8, &pid
) < 0)
3290 log_warning("Failed to parse notification message %s", e
);
3292 log_debug("%s: got %s", u
->id
, e
);
3293 service_set_main_pid(s
, pid
);
3297 /* Interpret READY= */
3298 if (s
->type
== SERVICE_NOTIFY
&&
3299 s
->state
== SERVICE_START
&&
3300 strv_find(tags
, "READY=1")) {
3301 log_debug("%s: got READY=1", u
->id
);
3303 service_enter_start_post(s
);
3306 /* Interpret STATUS= */
3307 e
= strv_find_prefix(tags
, "STATUS=");
3313 if (!utf8_is_valid(e
+7)) {
3314 log_warning("Status message in notification is not UTF-8 clean.");
3320 log_error("Failed to allocate string.");
3324 log_debug("%s: got %s", u
->id
, e
);
3326 free(s
->status_text
);
3329 free(s
->status_text
);
3330 s
->status_text
= NULL
;
3334 if (strv_find(tags
, "WATCHDOG=1")) {
3335 log_debug("%s: got WATCHDOG=1", u
->id
);
3336 service_reset_watchdog(s
);
3339 /* Notify clients about changed status or main pid */
3340 unit_add_to_dbus_queue(u
);
3343 #ifdef HAVE_SYSV_COMPAT
3346 static void sysv_facility_in_insserv_conf(Manager
*mgr
) {
3350 if (!(f
= fopen("/etc/insserv.conf", "re"))) {
3351 r
= errno
== ENOENT
? 0 : -errno
;
3356 char l
[LINE_MAX
], *t
;
3357 char **parsed
= NULL
;
3359 if (!fgets(l
, sizeof(l
), f
)) {
3364 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r
));
3369 if (*t
!= '$' && *t
!= '<')
3372 parsed
= strv_split(t
,WHITESPACE
);
3373 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3374 if (parsed
&& !startswith_no_case (parsed
[0], "<interactive>")) {
3377 if (sysv_translate_facility(parsed
[0], NULL
, &facility
) < 0)
3379 if ((u
= manager_get_unit(mgr
, facility
)) && (u
->type
== UNIT_TARGET
)) {
3381 char *dep
= NULL
, *name
, **j
;
3383 STRV_FOREACH (j
, parsed
+1) {
3392 if (sysv_translate_facility(name
, NULL
, &dep
) < 0)
3395 r
= unit_add_two_dependencies_by_name(u
, UNIT_BEFORE
, e
, dep
, NULL
, true);
3410 static int service_enumerate(Manager
*m
) {
3414 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
3415 Set
*runlevel_services
[ELEMENTSOF(rcnd_table
)], *shutdown_services
= NULL
;
3422 if (m
->running_as
!= SYSTEMD_SYSTEM
)
3425 zero(runlevel_services
);
3427 STRV_FOREACH(p
, m
->lookup_paths
.sysvrcnd_path
)
3428 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++) {
3432 path
= strjoin(*p
, "/", rcnd_table
[i
].path
, NULL
);
3441 if (!(d
= opendir(path
))) {
3442 if (errno
!= ENOENT
)
3443 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
3448 while ((de
= readdir(d
))) {
3451 if (ignore_file(de
->d_name
))
3454 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
3457 if (strlen(de
->d_name
) < 4)
3460 a
= undecchar(de
->d_name
[1]);
3461 b
= undecchar(de
->d_name
[2]);
3467 fpath
= strjoin(path
, "/", de
->d_name
, NULL
);
3473 if (access(fpath
, X_OK
) < 0) {
3475 if (errno
!= ENOENT
)
3476 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
3482 if (!(name
= sysv_translate_name(de
->d_name
+ 3))) {
3487 if ((r
= manager_load_unit_prepare(m
, name
, NULL
, NULL
, &service
)) < 0) {
3488 log_warning("Failed to prepare unit %s: %s", name
, strerror(-r
));
3492 if (de
->d_name
[0] == 'S') {
3494 if (rcnd_table
[i
].type
== RUNLEVEL_UP
) {
3495 SERVICE(service
)->sysv_start_priority_from_rcnd
=
3496 MAX(a
*10 + b
, SERVICE(service
)->sysv_start_priority_from_rcnd
);
3498 SERVICE(service
)->sysv_enabled
= true;
3501 if ((r
= set_ensure_allocated(&runlevel_services
[i
], trivial_hash_func
, trivial_compare_func
)) < 0)
3504 if ((r
= set_put(runlevel_services
[i
], service
)) < 0)
3507 } else if (de
->d_name
[0] == 'K' &&
3508 (rcnd_table
[i
].type
== RUNLEVEL_DOWN
)) {
3510 if ((r
= set_ensure_allocated(&shutdown_services
, trivial_hash_func
, trivial_compare_func
)) < 0)
3513 if ((r
= set_put(shutdown_services
, service
)) < 0)
3519 /* Now we loaded all stubs and are aware of the lowest
3520 start-up priority for all services, not let's actually load
3521 the services, this will also tell us which services are
3522 actually native now */
3523 manager_dispatch_load_queue(m
);
3525 /* If this is a native service, rely on native ways to pull in
3526 * a service, don't pull it in via sysv rcN.d links. */
3527 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
3528 SET_FOREACH(service
, runlevel_services
[i
], j
) {
3529 service
= unit_follow_merge(service
);
3531 if (service
->fragment_path
)
3534 if ((r
= unit_add_two_dependencies_by_name_inverse(service
, UNIT_AFTER
, UNIT_WANTS
, rcnd_table
[i
].target
, NULL
, true)) < 0)
3538 /* We honour K links only for halt/reboot. For the normal
3539 * runlevels we assume the stop jobs will be implicitly added
3540 * by the core logic. Also, we don't really distinguish here
3541 * between the runlevels 0 and 6 and just add them to the
3542 * special shutdown target. */
3543 SET_FOREACH(service
, shutdown_services
, j
) {
3544 service
= unit_follow_merge(service
);
3546 if (service
->fragment_path
)
3549 if ((r
= unit_add_two_dependencies_by_name(service
, UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true)) < 0)
3556 sysv_facility_in_insserv_conf (m
);
3564 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
3565 set_free(runlevel_services
[i
]);
3566 set_free(shutdown_services
);
3575 static void service_bus_name_owner_change(
3578 const char *old_owner
,
3579 const char *new_owner
) {
3581 Service
*s
= SERVICE(u
);
3586 assert(streq(s
->bus_name
, name
));
3587 assert(old_owner
|| new_owner
);
3589 if (old_owner
&& new_owner
)
3590 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->id
, name
, old_owner
, new_owner
);
3592 log_debug("%s's D-Bus name %s no longer registered by %s", u
->id
, name
, old_owner
);
3594 log_debug("%s's D-Bus name %s now registered by %s", u
->id
, name
, new_owner
);
3596 s
->bus_name_good
= !!new_owner
;
3598 if (s
->type
== SERVICE_DBUS
) {
3600 /* service_enter_running() will figure out what to
3602 if (s
->state
== SERVICE_RUNNING
)
3603 service_enter_running(s
, SERVICE_SUCCESS
);
3604 else if (s
->state
== SERVICE_START
&& new_owner
)
3605 service_enter_start_post(s
);
3607 } else if (new_owner
&&
3609 (s
->state
== SERVICE_START
||
3610 s
->state
== SERVICE_START_POST
||
3611 s
->state
== SERVICE_RUNNING
||
3612 s
->state
== SERVICE_RELOAD
)) {
3614 /* Try to acquire PID from bus service */
3615 log_debug("Trying to acquire PID from D-Bus name...");
3617 bus_query_pid(u
->manager
, name
);
3621 static void service_bus_query_pid_done(
3626 Service
*s
= SERVICE(u
);
3631 log_debug("%s's D-Bus name %s is now owned by process %u", u
->id
, name
, (unsigned) pid
);
3633 if (s
->main_pid
<= 0 &&
3634 (s
->state
== SERVICE_START
||
3635 s
->state
== SERVICE_START_POST
||
3636 s
->state
== SERVICE_RUNNING
||
3637 s
->state
== SERVICE_RELOAD
))
3638 service_set_main_pid(s
, pid
);
3641 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
) {
3646 /* This is called by the socket code when instantiating a new
3647 * service for a stream socket and the socket needs to be
3650 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3653 if (s
->socket_fd
>= 0)
3656 if (s
->state
!= SERVICE_DEAD
)
3660 s
->got_socket_fd
= true;
3662 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3664 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3667 static void service_reset_failed(Unit
*u
) {
3668 Service
*s
= SERVICE(u
);
3672 if (s
->state
== SERVICE_FAILED
)
3673 service_set_state(s
, SERVICE_DEAD
);
3675 s
->result
= SERVICE_SUCCESS
;
3676 s
->reload_result
= SERVICE_SUCCESS
;
3678 RATELIMIT_RESET(s
->start_limit
);
3681 static int service_kill(Unit
*u
, KillWho who
, int signo
, DBusError
*error
) {
3682 Service
*s
= SERVICE(u
);
3684 Set
*pid_set
= NULL
;
3688 if (s
->main_pid
<= 0 && who
== KILL_MAIN
) {
3689 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No main process to kill");
3693 if (s
->control_pid
<= 0 && who
== KILL_CONTROL
) {
3694 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No control process to kill");
3698 if (who
== KILL_CONTROL
|| who
== KILL_ALL
)
3699 if (s
->control_pid
> 0)
3700 if (kill(s
->control_pid
, signo
) < 0)
3703 if (who
== KILL_MAIN
|| who
== KILL_ALL
)
3704 if (s
->main_pid
> 0)
3705 if (kill(s
->main_pid
, signo
) < 0)
3708 if (who
== KILL_ALL
) {
3711 pid_set
= set_new(trivial_hash_func
, trivial_compare_func
);
3715 /* Exclude the control/main pid from being killed via the cgroup */
3716 if (s
->control_pid
> 0) {
3717 q
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
));
3724 if (s
->main_pid
> 0) {
3725 q
= set_put(pid_set
, LONG_TO_PTR(s
->main_pid
));
3732 q
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, signo
, false, false, pid_set
, NULL
);
3733 if (q
< 0 && q
!= -EAGAIN
&& q
!= -ESRCH
&& q
!= -ENOENT
)
3744 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
3745 [SERVICE_DEAD
] = "dead",
3746 [SERVICE_START_PRE
] = "start-pre",
3747 [SERVICE_START
] = "start",
3748 [SERVICE_START_POST
] = "start-post",
3749 [SERVICE_RUNNING
] = "running",
3750 [SERVICE_EXITED
] = "exited",
3751 [SERVICE_RELOAD
] = "reload",
3752 [SERVICE_STOP
] = "stop",
3753 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
3754 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
3755 [SERVICE_STOP_POST
] = "stop-post",
3756 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
3757 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
3758 [SERVICE_FAILED
] = "failed",
3759 [SERVICE_AUTO_RESTART
] = "auto-restart",
3762 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
3764 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3765 [SERVICE_RESTART_NO
] = "no",
3766 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3767 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3768 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3769 [SERVICE_RESTART_ALWAYS
] = "always"
3772 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3774 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3775 [SERVICE_SIMPLE
] = "simple",
3776 [SERVICE_FORKING
] = "forking",
3777 [SERVICE_ONESHOT
] = "oneshot",
3778 [SERVICE_DBUS
] = "dbus",
3779 [SERVICE_NOTIFY
] = "notify",
3780 [SERVICE_IDLE
] = "idle"
3783 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3785 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3786 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3787 [SERVICE_EXEC_START
] = "ExecStart",
3788 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3789 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3790 [SERVICE_EXEC_STOP
] = "ExecStop",
3791 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3794 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3796 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3797 [NOTIFY_NONE
] = "none",
3798 [NOTIFY_MAIN
] = "main",
3799 [NOTIFY_ALL
] = "all"
3802 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3804 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3805 [SERVICE_SUCCESS
] = "success",
3806 [SERVICE_FAILURE_RESOURCES
] = "resources",
3807 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3808 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3809 [SERVICE_FAILURE_SIGNAL
] = "signal",
3810 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3811 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3812 [SERVICE_FAILURE_START_LIMIT
] = "start-limit"
3815 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3817 static const char* const start_limit_action_table
[_SERVICE_START_LIMIT_MAX
] = {
3818 [SERVICE_START_LIMIT_NONE
] = "none",
3819 [SERVICE_START_LIMIT_REBOOT
] = "reboot",
3820 [SERVICE_START_LIMIT_REBOOT_FORCE
] = "reboot-force",
3821 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE
] = "reboot-immediate"
3823 DEFINE_STRING_TABLE_LOOKUP(start_limit_action
, StartLimitAction
);
3825 const UnitVTable service_vtable
= {
3826 .object_size
= sizeof(Service
),
3827 .exec_context_offset
= offsetof(Service
, exec_context
),
3834 .init
= service_init
,
3835 .done
= service_done
,
3836 .load
= service_load
,
3838 .coldplug
= service_coldplug
,
3840 .dump
= service_dump
,
3842 .start
= service_start
,
3843 .stop
= service_stop
,
3844 .reload
= service_reload
,
3846 .can_reload
= service_can_reload
,
3848 .kill
= service_kill
,
3850 .serialize
= service_serialize
,
3851 .deserialize_item
= service_deserialize_item
,
3853 .active_state
= service_active_state
,
3854 .sub_state_to_string
= service_sub_state_to_string
,
3856 .check_gc
= service_check_gc
,
3857 .check_snapshot
= service_check_snapshot
,
3859 .sigchld_event
= service_sigchld_event
,
3860 .timer_event
= service_timer_event
,
3861 .fd_event
= service_fd_event
,
3863 .reset_failed
= service_reset_failed
,
3865 .cgroup_notify_empty
= service_cgroup_notify_event
,
3866 .notify_message
= service_notify_message
,
3868 .bus_name_owner_change
= service_bus_name_owner_change
,
3869 .bus_query_pid_done
= service_bus_query_pid_done
,
3871 .bus_interface
= "org.freedesktop.systemd1.Service",
3872 .bus_message_handler
= bus_service_message_handler
,
3873 .bus_invalidating_properties
= bus_service_invalidating_properties
,
3875 #ifdef HAVE_SYSV_COMPAT
3876 .enumerate
= service_enumerate
,
3878 .status_message_formats
= {
3879 .starting_stopping
= {
3880 [0] = "Starting %s...",
3881 [1] = "Stopping %s...",
3883 .finished_start_job
= {
3884 [JOB_DONE
] = "Started %s.",
3885 [JOB_FAILED
] = "Failed to start %s.",
3886 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
3887 [JOB_TIMEOUT
] = "Timed out starting %s.",
3889 .finished_stop_job
= {
3890 [JOB_DONE
] = "Stopped %s.",
3891 [JOB_FAILED
] = "Stopped (with error) %s.",
3892 [JOB_TIMEOUT
] = "Timed out stopping %s.",