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 "dbus-service.h"
38 #include "bus-errors.h"
39 #include "exit-status.h"
41 #include "path-util.h"
45 #ifdef HAVE_SYSV_COMPAT
47 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
49 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
},
68 /* SUSE style boot.d */
69 { "boot.d", SPECIAL_SYSINIT_TARGET
, RUNLEVEL_SYSINIT
},
72 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
73 /* Debian style rcS.d */
74 { "rcS.d", SPECIAL_SYSINIT_TARGET
, RUNLEVEL_SYSINIT
},
77 /* Standard SysV runlevels for shutdown */
78 { "rc0.d", SPECIAL_POWEROFF_TARGET
, RUNLEVEL_DOWN
},
79 { "rc6.d", SPECIAL_REBOOT_TARGET
, RUNLEVEL_DOWN
}
81 /* Note that the order here matters, as we read the
82 directories in this order, and we want to make sure that
83 sysv_start_priority is known when we first load the
84 unit. And that value we only know from S links. Hence
85 UP/SYSINIT must be read before DOWN */
88 #define RUNLEVELS_UP "12345"
89 /* #define RUNLEVELS_DOWN "06" */
90 #define RUNLEVELS_BOOT "bBsS"
93 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
94 [SERVICE_DEAD
] = UNIT_INACTIVE
,
95 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
96 [SERVICE_START
] = UNIT_ACTIVATING
,
97 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
98 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
99 [SERVICE_EXITED
] = UNIT_ACTIVE
,
100 [SERVICE_RELOAD
] = UNIT_RELOADING
,
101 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
102 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
103 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
104 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
105 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
106 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
107 [SERVICE_FAILED
] = UNIT_FAILED
,
108 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
111 /* For Type=idle we never want to delay any other jobs, hence we
112 * consider idle jobs active as soon as we start working on them */
113 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
114 [SERVICE_DEAD
] = UNIT_INACTIVE
,
115 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
116 [SERVICE_START
] = UNIT_ACTIVE
,
117 [SERVICE_START_POST
] = UNIT_ACTIVE
,
118 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
119 [SERVICE_EXITED
] = UNIT_ACTIVE
,
120 [SERVICE_RELOAD
] = UNIT_RELOADING
,
121 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
122 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
123 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
124 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
125 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
126 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
127 [SERVICE_FAILED
] = UNIT_FAILED
,
128 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
131 static void service_init(Unit
*u
) {
132 Service
*s
= SERVICE(u
);
136 assert(u
->load_state
== UNIT_STUB
);
138 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
139 s
->restart_usec
= DEFAULT_RESTART_USEC
;
140 s
->type
= _SERVICE_TYPE_INVALID
;
142 s
->watchdog_watch
.type
= WATCH_INVALID
;
144 s
->timer_watch
.type
= WATCH_INVALID
;
145 #ifdef HAVE_SYSV_COMPAT
146 s
->sysv_start_priority
= -1;
147 s
->sysv_start_priority_from_rcnd
= -1;
150 s
->guess_main_pid
= true;
152 exec_context_init(&s
->exec_context
);
153 for (i
= 0; i
< RLIMIT_NLIMITS
; i
++)
154 if (UNIT(s
)->manager
->rlimit
[i
])
155 s
->exec_context
.rlimit
[i
] = newdup(struct rlimit
, UNIT(s
)->manager
->rlimit
[i
], 1);
156 kill_context_init(&s
->kill_context
);
158 RATELIMIT_INIT(s
->start_limit
, 10*USEC_PER_SEC
, 5);
160 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
163 static void service_unwatch_control_pid(Service
*s
) {
166 if (s
->control_pid
<= 0)
169 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
173 static void service_unwatch_main_pid(Service
*s
) {
176 if (s
->main_pid
<= 0)
179 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
183 static void service_unwatch_pid_file(Service
*s
) {
184 if (!s
->pid_file_pathspec
)
187 log_debug("Stopping watch for %s's PID file %s", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
188 path_spec_unwatch(s
->pid_file_pathspec
, UNIT(s
));
189 path_spec_done(s
->pid_file_pathspec
);
190 free(s
->pid_file_pathspec
);
191 s
->pid_file_pathspec
= NULL
;
194 static int service_set_main_pid(Service
*s
, pid_t pid
) {
206 s
->main_pid_known
= true;
208 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
209 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
210 UNIT(s
)->id
, (unsigned long) pid
);
212 s
->main_pid_alien
= true;
214 s
->main_pid_alien
= false;
216 exec_status_start(&s
->main_exec_status
, pid
);
221 static void service_close_socket_fd(Service
*s
) {
224 if (s
->socket_fd
< 0)
227 close_nointr_nofail(s
->socket_fd
);
231 static void service_connection_unref(Service
*s
) {
234 if (!UNIT_DEREF(s
->accept_socket
))
237 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
238 unit_ref_unset(&s
->accept_socket
);
241 static void service_stop_watchdog(Service
*s
) {
244 unit_unwatch_timer(UNIT(s
), &s
->watchdog_watch
);
245 s
->watchdog_timestamp
.realtime
= 0;
246 s
->watchdog_timestamp
.monotonic
= 0;
249 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
);
251 static void service_handle_watchdog(Service
*s
) {
257 if (s
->watchdog_usec
== 0)
260 offset
= now(CLOCK_MONOTONIC
) - s
->watchdog_timestamp
.monotonic
;
261 if (offset
>= s
->watchdog_usec
) {
262 log_error("%s watchdog timeout!", UNIT(s
)->id
);
263 service_enter_dead(s
, SERVICE_FAILURE_WATCHDOG
, true);
267 r
= unit_watch_timer(UNIT(s
), s
->watchdog_usec
- offset
, &s
->watchdog_watch
);
269 log_warning("%s failed to install watchdog timer: %s", UNIT(s
)->id
, strerror(-r
));
272 static void service_reset_watchdog(Service
*s
) {
275 dual_timestamp_get(&s
->watchdog_timestamp
);
276 service_handle_watchdog(s
);
279 static void service_done(Unit
*u
) {
280 Service
*s
= SERVICE(u
);
287 #ifdef HAVE_SYSV_COMPAT
288 free(s
->sysv_runlevels
);
289 s
->sysv_runlevels
= NULL
;
292 free(s
->status_text
);
293 s
->status_text
= NULL
;
295 exec_context_done(&s
->exec_context
);
296 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
297 s
->control_command
= NULL
;
298 s
->main_command
= NULL
;
300 /* This will leak a process, but at least no memory or any of
302 service_unwatch_main_pid(s
);
303 service_unwatch_control_pid(s
);
304 service_unwatch_pid_file(s
);
307 unit_unwatch_bus_name(u
, s
->bus_name
);
312 service_close_socket_fd(s
);
313 service_connection_unref(s
);
315 unit_ref_unset(&s
->accept_socket
);
317 service_stop_watchdog(s
);
319 unit_unwatch_timer(u
, &s
->timer_watch
);
322 #ifdef HAVE_SYSV_COMPAT
323 static char *sysv_translate_name(const char *name
) {
326 if (!(r
= new(char, strlen(name
) + sizeof(".service"))))
329 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
330 if (endswith(name
, ".sh"))
331 /* Drop Debian-style .sh suffix */
332 strcpy(stpcpy(r
, name
) - 3, ".service");
335 if (startswith(name
, "boot."))
336 /* Drop SuSE-style boot. prefix */
337 strcpy(stpcpy(r
, name
+ 5), ".service");
339 #ifdef TARGET_FRUGALWARE
340 if (startswith(name
, "rc."))
341 /* Drop Frugalware-style rc. prefix */
342 strcpy(stpcpy(r
, name
+ 3), ".service");
345 /* Normal init scripts */
346 strcpy(stpcpy(r
, name
), ".service");
351 static int sysv_translate_facility(const char *name
, const char *filename
, char **_r
) {
353 /* We silently ignore the $ prefix here. According to the LSB
354 * spec it simply indicates whether something is a
355 * standardized name or a distribution-specific one. Since we
356 * just follow what already exists and do not introduce new
357 * uses or names we don't care who introduced a new name. */
359 static const char * const table
[] = {
360 /* LSB defined facilities */
361 "local_fs", SPECIAL_LOCAL_FS_TARGET
,
362 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
364 /* Due to unfortunate name selection in Mandriva,
365 * $network is provided by network-up which is ordered
366 * after network which actually starts interfaces.
367 * To break the loop, just ignore it */
368 "network", SPECIAL_NETWORK_TARGET
,
370 "named", SPECIAL_NSS_LOOKUP_TARGET
,
371 "portmap", SPECIAL_RPCBIND_TARGET
,
372 "remote_fs", SPECIAL_REMOTE_FS_TARGET
,
373 "syslog", SPECIAL_SYSLOG_TARGET
,
374 "time", SPECIAL_TIME_SYNC_TARGET
,
376 /* common extensions */
377 "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
378 "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
,
381 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
382 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
386 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
387 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
388 "httpd", SPECIAL_HTTP_DAEMON_TARGET
,
392 "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET
,
403 n
= *name
== '$' ? name
+ 1 : name
;
405 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2) {
407 if (!streq(table
[i
], n
))
413 if (!(r
= strdup(table
[i
+1])))
419 /* If we don't know this name, fallback heuristics to figure
420 * out whether something is a target or a service alias. */
423 if (!unit_prefix_is_valid(n
))
426 /* Facilities starting with $ are most likely targets */
427 r
= unit_name_build(n
, NULL
, ".target");
428 } else if (filename
&& streq(name
, filename
))
429 /* Names equaling the file name of the services are redundant */
432 /* Everything else we assume to be normal service names */
433 r
= sysv_translate_name(n
);
444 static int sysv_fix_order(Service
*s
) {
450 if (s
->sysv_start_priority
< 0)
453 /* For each pair of services where at least one lacks a LSB
454 * header, we use the start priority value to order things. */
456 LIST_FOREACH(units_by_type
, other
, UNIT(s
)->manager
->units_by_type
[UNIT_SERVICE
]) {
459 bool special_s
, special_t
;
466 if (UNIT(t
)->load_state
!= UNIT_LOADED
)
469 if (t
->sysv_start_priority
< 0)
472 /* If both units have modern headers we don't care
473 * about the priorities */
474 if ((UNIT(s
)->fragment_path
|| s
->sysv_has_lsb
) &&
475 (UNIT(t
)->fragment_path
|| t
->sysv_has_lsb
))
478 special_s
= s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
);
479 special_t
= t
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, t
->sysv_runlevels
);
481 if (special_t
&& !special_s
)
483 else if (special_s
&& !special_t
)
485 else if (t
->sysv_start_priority
< s
->sysv_start_priority
)
487 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
492 /* FIXME: Maybe we should compare the name here lexicographically? */
494 if ((r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
501 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
504 if (!(c
= new0(ExecCommand
, 1)))
507 if (!(c
->path
= strdup(path
))) {
512 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
521 static int sysv_exec_commands(Service
*s
) {
526 assert(UNIT(s
)->source_path
);
528 c
= exec_command_new(UNIT(s
)->source_path
, "start");
531 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
533 c
= exec_command_new(UNIT(s
)->source_path
, "stop");
536 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
538 c
= exec_command_new(UNIT(s
)->source_path
, "reload");
541 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
546 static int service_load_sysv_path(Service
*s
, const char *path
) {
557 char *short_description
= NULL
, *long_description
= NULL
, *chkconfig_description
= NULL
, *description
;
565 f
= fopen(path
, "re");
567 r
= errno
== ENOENT
? 0 : -errno
;
572 if (fstat(fileno(f
), &st
) < 0) {
577 free(u
->source_path
);
578 u
->source_path
= strdup(path
);
579 if (!u
->source_path
) {
583 u
->source_mtime
= timespec_load(&st
.st_mtim
);
585 if (null_or_empty(&st
)) {
586 u
->load_state
= UNIT_MASKED
;
594 char l
[LINE_MAX
], *t
;
596 if (!fgets(l
, sizeof(l
), f
)) {
601 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
611 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
613 s
->sysv_has_lsb
= true;
617 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
623 t
+= strspn(t
, WHITESPACE
);
625 if (state
== NORMAL
) {
627 /* Try to parse Red Hat style chkconfig headers */
629 if (startswith_no_case(t
, "chkconfig:")) {
631 char runlevels
[16], *k
;
635 if (sscanf(t
+10, "%15s %i %*i",
637 &start_priority
) != 2) {
639 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
643 /* A start priority gathered from the
644 * symlink farms is preferred over the
645 * data from the LSB header. */
646 if (start_priority
< 0 || start_priority
> 99)
647 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
649 s
->sysv_start_priority
= start_priority
;
651 char_array_0(runlevels
);
652 k
= delete_chars(runlevels
, WHITESPACE
"-");
657 if (!(d
= strdup(k
))) {
662 free(s
->sysv_runlevels
);
663 s
->sysv_runlevels
= d
;
666 } else if (startswith_no_case(t
, "description:")) {
668 size_t k
= strlen(t
);
672 if (t
[k
-1] == '\\') {
677 if ((j
= strstrip(t
+12)) && *j
) {
678 if (!(d
= strdup(j
))) {
685 free(chkconfig_description
);
686 chkconfig_description
= d
;
688 } else if (startswith_no_case(t
, "pidfile:")) {
695 if (!path_is_absolute(fn
)) {
696 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
700 if (!(fn
= strdup(fn
))) {
709 } else if (state
== DESCRIPTION
) {
711 /* Try to parse Red Hat style description
714 size_t k
= strlen(t
);
722 if ((j
= strstrip(t
)) && *j
) {
725 if (chkconfig_description
)
726 d
= strjoin(chkconfig_description
, " ", j
, NULL
);
735 free(chkconfig_description
);
736 chkconfig_description
= d
;
739 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
741 if (startswith_no_case(t
, "Provides:")) {
747 FOREACH_WORD_QUOTED(w
, z
, t
+9, i
) {
750 if (!(n
= strndup(w
, z
))) {
755 r
= sysv_translate_facility(n
, path_get_file_name(path
), &m
);
764 if (unit_name_to_type(m
) == UNIT_SERVICE
)
765 r
= unit_add_name(u
, m
);
772 * indication that the
774 * now available. This
777 * targets do NOT pull
780 r
= unit_add_two_dependencies_by_name(u
, UNIT_BEFORE
, UNIT_WANTS
, m
, NULL
, true);
783 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path
, line
, m
, strerror(-r
));
788 } else if (startswith_no_case(t
, "Required-Start:") ||
789 startswith_no_case(t
, "Should-Start:") ||
790 startswith_no_case(t
, "X-Start-Before:") ||
791 startswith_no_case(t
, "X-Start-After:")) {
797 FOREACH_WORD_QUOTED(w
, z
, strchr(t
, ':')+1, i
) {
800 if (!(n
= strndup(w
, z
))) {
805 r
= sysv_translate_facility(n
, path_get_file_name(path
), &m
);
808 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path
, line
, n
, strerror(-r
));
818 r
= unit_add_dependency_by_name(u
, startswith_no_case(t
, "X-Start-Before:") ? UNIT_BEFORE
: UNIT_AFTER
, m
, NULL
, true);
821 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path
, line
, m
, strerror(-r
));
825 } else if (startswith_no_case(t
, "Default-Start:")) {
830 k
= delete_chars(t
+14, WHITESPACE
"-");
833 if (!(d
= strdup(k
))) {
838 free(s
->sysv_runlevels
);
839 s
->sysv_runlevels
= d
;
842 } else if (startswith_no_case(t
, "Description:")) {
845 state
= LSB_DESCRIPTION
;
847 if ((j
= strstrip(t
+12)) && *j
) {
848 if (!(d
= strdup(j
))) {
855 free(long_description
);
856 long_description
= d
;
858 } else if (startswith_no_case(t
, "Short-Description:")) {
863 if ((j
= strstrip(t
+18)) && *j
) {
864 if (!(d
= strdup(j
))) {
871 free(short_description
);
872 short_description
= d
;
874 } else if (state
== LSB_DESCRIPTION
) {
876 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
879 if ((j
= strstrip(t
)) && *j
) {
882 if (long_description
)
883 d
= strjoin(long_description
, " ", t
, NULL
);
892 free(long_description
);
893 long_description
= d
;
902 if ((r
= sysv_exec_commands(s
)) < 0)
904 if (s
->sysv_runlevels
&&
905 chars_intersect(RUNLEVELS_BOOT
, s
->sysv_runlevels
) &&
906 chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
)) {
907 /* Service has both boot and "up" runlevels
908 configured. Kill the "up" ones. */
909 delete_chars(s
->sysv_runlevels
, RUNLEVELS_UP
);
912 if (s
->sysv_runlevels
&& !chars_intersect(RUNLEVELS_UP
, s
->sysv_runlevels
)) {
913 /* If there a runlevels configured for this service
914 * but none of the standard ones, then we assume this
915 * is some special kind of service (which might be
916 * needed for early boot) and don't create any links
919 UNIT(s
)->default_dependencies
= false;
921 /* Don't timeout special services during boot (like fsck) */
924 s
->timeout_usec
= DEFAULT_SYSV_TIMEOUT_USEC
;
926 /* Special setting for all SysV services */
927 s
->type
= SERVICE_FORKING
;
928 s
->remain_after_exit
= !s
->pid_file
;
929 s
->guess_main_pid
= false;
930 s
->restart
= SERVICE_RESTART_NO
;
931 s
->exec_context
.ignore_sigpipe
= false;
932 s
->kill_context
.kill_mode
= KILL_PROCESS
;
934 /* We use the long description only if
935 * no short description is set. */
937 if (short_description
)
938 description
= short_description
;
939 else if (chkconfig_description
)
940 description
= chkconfig_description
;
941 else if (long_description
)
942 description
= long_description
;
949 if (!(d
= strappend(s
->sysv_has_lsb
? "LSB: " : "SYSV: ", description
))) {
957 /* The priority that has been set in /etc/rcN.d/ hierarchies
958 * takes precedence over what is stored as default in the LSB
960 if (s
->sysv_start_priority_from_rcnd
>= 0)
961 s
->sysv_start_priority
= s
->sysv_start_priority_from_rcnd
;
963 u
->load_state
= UNIT_LOADED
;
971 free(short_description
);
972 free(long_description
);
973 free(chkconfig_description
);
978 static int service_load_sysv_name(Service
*s
, const char *name
) {
984 /* For SysV services we strip the boot.*, rc.* and *.sh
985 * prefixes/suffixes. */
986 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
987 if (endswith(name
, ".sh.service"))
992 if (startswith(name
, "boot."))
996 #ifdef TARGET_FRUGALWARE
997 if (startswith(name
, "rc."))
1001 STRV_FOREACH(p
, UNIT(s
)->manager
->lookup_paths
.sysvinit_path
) {
1005 path
= strjoin(*p
, "/", name
, NULL
);
1009 assert(endswith(path
, ".service"));
1010 path
[strlen(path
)-8] = 0;
1012 r
= service_load_sysv_path(s
, path
);
1014 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
1015 if (r
>= 0 && UNIT(s
)->load_state
== UNIT_STUB
) {
1016 /* Try Debian style *.sh source'able init scripts */
1017 strcat(path
, ".sh");
1018 r
= service_load_sysv_path(s
, path
);
1024 if (r
>= 0 && UNIT(s
)->load_state
== UNIT_STUB
) {
1025 /* Try SUSE style boot.* init scripts */
1027 path
= strjoin(*p
, "/boot.", name
, NULL
);
1031 /* Drop .service suffix */
1032 path
[strlen(path
)-8] = 0;
1033 r
= service_load_sysv_path(s
, path
);
1038 #ifdef TARGET_FRUGALWARE
1039 if (r
>= 0 && UNIT(s
)->load_state
== UNIT_STUB
) {
1040 /* Try Frugalware style rc.* init scripts */
1042 path
= strjoin(*p
, "/rc.", name
, NULL
);
1046 /* Drop .service suffix */
1047 path
[strlen(path
)-8] = 0;
1048 r
= service_load_sysv_path(s
, path
);
1056 if ((UNIT(s
)->load_state
!= UNIT_STUB
))
1063 static int service_load_sysv(Service
*s
) {
1070 /* Load service data from SysV init scripts, preferably with
1071 * LSB headers ... */
1073 if (strv_isempty(UNIT(s
)->manager
->lookup_paths
.sysvinit_path
))
1076 if ((t
= UNIT(s
)->id
))
1077 if ((r
= service_load_sysv_name(s
, t
)) < 0)
1080 if (UNIT(s
)->load_state
== UNIT_STUB
)
1081 SET_FOREACH(t
, UNIT(s
)->names
, i
) {
1082 if (t
== UNIT(s
)->id
)
1085 if ((r
= service_load_sysv_name(s
, t
)) < 0)
1088 if (UNIT(s
)->load_state
!= UNIT_STUB
)
1096 static int fsck_fix_order(Service
*s
) {
1102 if (s
->fsck_passno
<= 0)
1105 /* For each pair of services where both have an fsck priority
1106 * we order things based on it. */
1108 LIST_FOREACH(units_by_type
, other
, UNIT(s
)->manager
->units_by_type
[UNIT_SERVICE
]) {
1117 if (UNIT(t
)->load_state
!= UNIT_LOADED
)
1120 if (t
->fsck_passno
<= 0)
1123 if (t
->fsck_passno
< s
->fsck_passno
)
1125 else if (t
->fsck_passno
> s
->fsck_passno
)
1130 if ((r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
), true)) < 0)
1137 static int service_verify(Service
*s
) {
1140 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
1143 if (!s
->exec_command
[SERVICE_EXEC_START
]) {
1144 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s
)->id
);
1148 if (s
->type
!= SERVICE_ONESHOT
&&
1149 s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
1150 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
1154 if (s
->type
== SERVICE_ONESHOT
&&
1155 s
->exec_command
[SERVICE_EXEC_RELOAD
]) {
1156 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
1160 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
1161 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s
)->id
);
1165 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
1166 log_warning("%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s
)->id
);
1168 if (s
->exec_context
.pam_name
&& s
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
1169 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s
)->id
);
1176 static int service_add_default_dependencies(Service
*s
) {
1181 /* Add a number of automatic dependencies useful for the
1182 * majority of services. */
1184 /* First, pull in base system */
1185 if (UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
) {
1187 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true)) < 0)
1190 } else if (UNIT(s
)->manager
->running_as
== MANAGER_USER
) {
1192 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SOCKETS_TARGET
, NULL
, true)) < 0)
1196 /* Second, activate normal shutdown */
1197 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
1200 static void service_fix_output(Service
*s
) {
1203 /* If nothing has been explicitly configured, patch default
1204 * output in. If input is socket/tty we avoid this however,
1205 * since in that case we want output to default to the same
1206 * place as we read input from. */
1208 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
1209 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
1210 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
1211 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
1213 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
1214 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
1215 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
1218 static int service_load(Unit
*u
) {
1220 Service
*s
= SERVICE(u
);
1224 /* Load a .service file */
1225 if ((r
= unit_load_fragment(u
)) < 0)
1228 #ifdef HAVE_SYSV_COMPAT
1229 /* Load a classic init script as a fallback, if we couldn't find anything */
1230 if (u
->load_state
== UNIT_STUB
)
1231 if ((r
= service_load_sysv(s
)) < 0)
1235 /* Still nothing found? Then let's give up */
1236 if (u
->load_state
== UNIT_STUB
)
1239 /* We were able to load something, then let's add in the
1240 * dropin directories. */
1241 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
1244 /* This is a new unit? Then let's add in some extras */
1245 if (u
->load_state
== UNIT_LOADED
) {
1246 if (s
->type
== _SERVICE_TYPE_INVALID
)
1247 s
->type
= s
->bus_name
? SERVICE_DBUS
: SERVICE_SIMPLE
;
1249 /* Oneshot services have disabled timeout by default */
1250 if (s
->type
== SERVICE_ONESHOT
&& !s
->timeout_defined
)
1251 s
->timeout_usec
= 0;
1253 service_fix_output(s
);
1255 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
1258 if ((r
= unit_add_default_cgroups(u
)) < 0)
1261 #ifdef HAVE_SYSV_COMPAT
1262 if ((r
= sysv_fix_order(s
)) < 0)
1266 if ((r
= fsck_fix_order(s
)) < 0)
1270 if ((r
= unit_watch_bus_name(u
, s
->bus_name
)) < 0)
1273 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
1274 s
->notify_access
= NOTIFY_MAIN
;
1276 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
1277 s
->notify_access
= NOTIFY_MAIN
;
1279 if (s
->type
== SERVICE_DBUS
|| s
->bus_name
)
1280 if ((r
= unit_add_two_dependencies_by_name(u
, UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, NULL
, true)) < 0)
1283 if (UNIT(s
)->default_dependencies
)
1284 if ((r
= service_add_default_dependencies(s
)) < 0)
1287 r
= unit_patch_working_directory(UNIT(s
), &s
->exec_context
);
1292 return service_verify(s
);
1295 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
1297 ServiceExecCommand c
;
1298 Service
*s
= SERVICE(u
);
1299 const char *prefix2
;
1304 p2
= strappend(prefix
, "\t");
1305 prefix2
= p2
? p2
: prefix
;
1308 "%sService State: %s\n"
1310 "%sReload Result: %s\n"
1311 "%sPermissionsStartOnly: %s\n"
1312 "%sRootDirectoryStartOnly: %s\n"
1313 "%sRemainAfterExit: %s\n"
1314 "%sGuessMainPID: %s\n"
1317 "%sNotifyAccess: %s\n",
1318 prefix
, service_state_to_string(s
->state
),
1319 prefix
, service_result_to_string(s
->result
),
1320 prefix
, service_result_to_string(s
->reload_result
),
1321 prefix
, yes_no(s
->permissions_start_only
),
1322 prefix
, yes_no(s
->root_directory_start_only
),
1323 prefix
, yes_no(s
->remain_after_exit
),
1324 prefix
, yes_no(s
->guess_main_pid
),
1325 prefix
, service_type_to_string(s
->type
),
1326 prefix
, service_restart_to_string(s
->restart
),
1327 prefix
, notify_access_to_string(s
->notify_access
));
1329 if (s
->control_pid
> 0)
1331 "%sControl PID: %lu\n",
1332 prefix
, (unsigned long) s
->control_pid
);
1334 if (s
->main_pid
> 0)
1337 "%sMain PID Known: %s\n"
1338 "%sMain PID Alien: %s\n",
1339 prefix
, (unsigned long) s
->main_pid
,
1340 prefix
, yes_no(s
->main_pid_known
),
1341 prefix
, yes_no(s
->main_pid_alien
));
1346 prefix
, s
->pid_file
);
1351 "%sBus Name Good: %s\n",
1352 prefix
, s
->bus_name
,
1353 prefix
, yes_no(s
->bus_name_good
));
1355 kill_context_dump(&s
->kill_context
, f
, prefix
);
1356 exec_context_dump(&s
->exec_context
, f
, prefix
);
1358 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
1360 if (!s
->exec_command
[c
])
1363 fprintf(f
, "%s-> %s:\n",
1364 prefix
, service_exec_command_to_string(c
));
1366 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
1369 #ifdef HAVE_SYSV_COMPAT
1372 "%sSysV Init Script has LSB Header: %s\n"
1373 "%sSysVEnabled: %s\n",
1374 prefix
, yes_no(s
->sysv_has_lsb
),
1375 prefix
, yes_no(s
->sysv_enabled
));
1377 if (s
->sysv_start_priority
>= 0)
1379 "%sSysVStartPriority: %i\n",
1380 prefix
, s
->sysv_start_priority
);
1382 if (s
->sysv_runlevels
)
1383 fprintf(f
, "%sSysVRunLevels: %s\n",
1384 prefix
, s
->sysv_runlevels
);
1387 if (s
->fsck_passno
> 0)
1389 "%sFsckPassNo: %i\n",
1390 prefix
, s
->fsck_passno
);
1393 fprintf(f
, "%sStatus Text: %s\n",
1394 prefix
, s
->status_text
);
1399 static int service_load_pid_file(Service
*s
, bool may_warn
) {
1409 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0) {
1411 log_info("PID file %s not readable (yet?) after %s.",
1412 s
->pid_file
, service_state_to_string(s
->state
));
1416 r
= parse_pid(k
, &pid
);
1422 if (kill(pid
, 0) < 0 && errno
!= EPERM
) {
1424 log_info("PID %lu read from file %s does not exist.",
1425 (unsigned long) pid
, s
->pid_file
);
1429 if (s
->main_pid_known
) {
1430 if (pid
== s
->main_pid
)
1433 log_debug("Main PID changing: %lu -> %lu",
1434 (unsigned long) s
->main_pid
, (unsigned long) pid
);
1435 service_unwatch_main_pid(s
);
1436 s
->main_pid_known
= false;
1438 log_debug("Main PID loaded: %lu", (unsigned long) pid
);
1440 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1443 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1444 /* FIXME: we need to do something here */
1450 static int service_search_main_pid(Service
*s
) {
1456 /* If we know it anyway, don't ever fallback to unreliable
1458 if (s
->main_pid_known
)
1461 if (!s
->guess_main_pid
)
1464 assert(s
->main_pid
<= 0);
1466 if ((pid
= cgroup_bonding_search_main_pid_list(UNIT(s
)->cgroup_bondings
)) <= 0)
1469 log_debug("Main PID guessed: %lu", (unsigned long) pid
);
1470 if ((r
= service_set_main_pid(s
, pid
)) < 0)
1473 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1474 /* FIXME: we need to do something here */
1480 static void service_notify_sockets_dead(Service
*s
, bool failed_permanent
) {
1486 /* Notifies all our sockets when we die */
1488 if (s
->socket_fd
>= 0)
1491 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
)
1492 if (u
->type
== UNIT_SOCKET
)
1493 socket_notify_service_dead(SOCKET(u
), failed_permanent
);
1498 static void service_set_state(Service
*s
, ServiceState state
) {
1499 ServiceState old_state
;
1500 const UnitActiveState
*table
;
1503 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
1505 old_state
= s
->state
;
1508 service_unwatch_pid_file(s
);
1510 if (state
!= SERVICE_START_PRE
&&
1511 state
!= SERVICE_START
&&
1512 state
!= SERVICE_START_POST
&&
1513 state
!= SERVICE_RELOAD
&&
1514 state
!= SERVICE_STOP
&&
1515 state
!= SERVICE_STOP_SIGTERM
&&
1516 state
!= SERVICE_STOP_SIGKILL
&&
1517 state
!= SERVICE_STOP_POST
&&
1518 state
!= SERVICE_FINAL_SIGTERM
&&
1519 state
!= SERVICE_FINAL_SIGKILL
&&
1520 state
!= SERVICE_AUTO_RESTART
)
1521 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1523 if (state
!= SERVICE_START
&&
1524 state
!= SERVICE_START_POST
&&
1525 state
!= SERVICE_RUNNING
&&
1526 state
!= SERVICE_RELOAD
&&
1527 state
!= SERVICE_STOP
&&
1528 state
!= SERVICE_STOP_SIGTERM
&&
1529 state
!= SERVICE_STOP_SIGKILL
) {
1530 service_unwatch_main_pid(s
);
1531 s
->main_command
= NULL
;
1534 if (state
!= SERVICE_START_PRE
&&
1535 state
!= SERVICE_START
&&
1536 state
!= SERVICE_START_POST
&&
1537 state
!= SERVICE_RELOAD
&&
1538 state
!= SERVICE_STOP
&&
1539 state
!= SERVICE_STOP_SIGTERM
&&
1540 state
!= SERVICE_STOP_SIGKILL
&&
1541 state
!= SERVICE_STOP_POST
&&
1542 state
!= SERVICE_FINAL_SIGTERM
&&
1543 state
!= SERVICE_FINAL_SIGKILL
) {
1544 service_unwatch_control_pid(s
);
1545 s
->control_command
= NULL
;
1546 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1549 if (state
== SERVICE_DEAD
||
1550 state
== SERVICE_STOP
||
1551 state
== SERVICE_STOP_SIGTERM
||
1552 state
== SERVICE_STOP_SIGKILL
||
1553 state
== SERVICE_STOP_POST
||
1554 state
== SERVICE_FINAL_SIGTERM
||
1555 state
== SERVICE_FINAL_SIGKILL
||
1556 state
== SERVICE_FAILED
||
1557 state
== SERVICE_AUTO_RESTART
)
1558 service_notify_sockets_dead(s
, false);
1560 if (state
!= SERVICE_START_PRE
&&
1561 state
!= SERVICE_START
&&
1562 state
!= SERVICE_START_POST
&&
1563 state
!= SERVICE_RUNNING
&&
1564 state
!= SERVICE_RELOAD
&&
1565 state
!= SERVICE_STOP
&&
1566 state
!= SERVICE_STOP_SIGTERM
&&
1567 state
!= SERVICE_STOP_SIGKILL
&&
1568 state
!= SERVICE_STOP_POST
&&
1569 state
!= SERVICE_FINAL_SIGTERM
&&
1570 state
!= SERVICE_FINAL_SIGKILL
&&
1571 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
1572 service_close_socket_fd(s
);
1573 service_connection_unref(s
);
1576 if (state
== SERVICE_STOP
)
1577 service_stop_watchdog(s
);
1579 /* For the inactive states unit_notify() will trim the cgroup,
1580 * but for exit we have to do that ourselves... */
1581 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
1582 cgroup_bonding_trim_list(UNIT(s
)->cgroup_bondings
, true);
1584 if (old_state
!= state
)
1585 log_debug("%s changed %s -> %s", UNIT(s
)->id
, service_state_to_string(old_state
), service_state_to_string(state
));
1587 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
1588 s
->reload_result
= SERVICE_SUCCESS
;
1591 static int service_coldplug(Unit
*u
) {
1592 Service
*s
= SERVICE(u
);
1596 assert(s
->state
== SERVICE_DEAD
);
1598 if (s
->deserialized_state
!= s
->state
) {
1600 if (s
->deserialized_state
== SERVICE_START_PRE
||
1601 s
->deserialized_state
== SERVICE_START
||
1602 s
->deserialized_state
== SERVICE_START_POST
||
1603 s
->deserialized_state
== SERVICE_RELOAD
||
1604 s
->deserialized_state
== SERVICE_STOP
||
1605 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1606 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1607 s
->deserialized_state
== SERVICE_STOP_POST
||
1608 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1609 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
||
1610 s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
1612 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
|| s
->timeout_usec
> 0) {
1615 k
= s
->deserialized_state
== SERVICE_AUTO_RESTART
? s
->restart_usec
: s
->timeout_usec
;
1617 if ((r
= unit_watch_timer(UNIT(s
), k
, &s
->timer_watch
)) < 0)
1622 if ((s
->deserialized_state
== SERVICE_START
&&
1623 (s
->type
== SERVICE_FORKING
||
1624 s
->type
== SERVICE_DBUS
||
1625 s
->type
== SERVICE_ONESHOT
||
1626 s
->type
== SERVICE_NOTIFY
)) ||
1627 s
->deserialized_state
== SERVICE_START_POST
||
1628 s
->deserialized_state
== SERVICE_RUNNING
||
1629 s
->deserialized_state
== SERVICE_RELOAD
||
1630 s
->deserialized_state
== SERVICE_STOP
||
1631 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1632 s
->deserialized_state
== SERVICE_STOP_SIGKILL
)
1633 if (s
->main_pid
> 0)
1634 if ((r
= unit_watch_pid(UNIT(s
), s
->main_pid
)) < 0)
1637 if (s
->deserialized_state
== SERVICE_START_PRE
||
1638 s
->deserialized_state
== SERVICE_START
||
1639 s
->deserialized_state
== SERVICE_START_POST
||
1640 s
->deserialized_state
== SERVICE_RELOAD
||
1641 s
->deserialized_state
== SERVICE_STOP
||
1642 s
->deserialized_state
== SERVICE_STOP_SIGTERM
||
1643 s
->deserialized_state
== SERVICE_STOP_SIGKILL
||
1644 s
->deserialized_state
== SERVICE_STOP_POST
||
1645 s
->deserialized_state
== SERVICE_FINAL_SIGTERM
||
1646 s
->deserialized_state
== SERVICE_FINAL_SIGKILL
)
1647 if (s
->control_pid
> 0)
1648 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1651 if (s
->deserialized_state
== SERVICE_START_POST
||
1652 s
->deserialized_state
== SERVICE_RUNNING
)
1653 service_handle_watchdog(s
);
1655 service_set_state(s
, s
->deserialized_state
);
1660 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1664 unsigned rn_fds
= 0;
1671 if (s
->socket_fd
>= 0)
1674 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1679 if (u
->type
!= UNIT_SOCKET
)
1684 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1696 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1702 memcpy(t
, rfds
, rn_fds
* sizeof(int));
1703 memcpy(t
+rn_fds
, cfds
, cn_fds
* sizeof(int));
1708 rn_fds
= rn_fds
+cn_fds
;
1723 static int service_spawn(
1728 bool apply_permissions
,
1730 bool apply_tty_stdin
,
1731 bool set_notify_socket
,
1737 int *fds
= NULL
, *fdsbuf
= NULL
;
1738 unsigned n_fds
= 0, n_env
= 0;
1739 char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
;
1746 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1747 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1748 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1750 if (s
->socket_fd
>= 0) {
1751 fds
= &s
->socket_fd
;
1754 if ((r
= service_collect_fds(s
, &fdsbuf
, &n_fds
)) < 0)
1761 if (timeout
&& s
->timeout_usec
) {
1762 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1765 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1767 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1772 if (!(our_env
= new0(char*, 4))) {
1777 if (set_notify_socket
)
1778 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1783 if (s
->main_pid
> 0)
1784 if (asprintf(our_env
+ n_env
++, "MAINPID=%lu", (unsigned long) s
->main_pid
) < 0) {
1789 if (s
->watchdog_usec
> 0)
1790 if (asprintf(our_env
+ n_env
++, "WATCHDOG_USEC=%llu", (unsigned long long) s
->watchdog_usec
) < 0) {
1795 if (!(final_env
= strv_env_merge(2,
1796 UNIT(s
)->manager
->environment
,
1811 UNIT(s
)->manager
->confirm_spawn
,
1812 UNIT(s
)->cgroup_bondings
,
1813 UNIT(s
)->cgroup_attributes
,
1814 is_control
? "control" : NULL
,
1816 s
->type
== SERVICE_IDLE
? UNIT(s
)->manager
->idle_pipe
: NULL
,
1823 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1824 /* FIXME: we need to do something here */
1830 strv_free(final_env
);
1840 strv_free(final_env
);
1843 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1848 static int main_pid_good(Service
*s
) {
1851 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1854 /* If we know the pid file, then lets just check if it is
1856 if (s
->main_pid_known
) {
1858 /* If it's an alien child let's check if it is still
1860 if (s
->main_pid_alien
)
1861 return kill(s
->main_pid
, 0) >= 0 || errno
!= ESRCH
;
1863 /* .. otherwise assume we'll get a SIGCHLD for it,
1864 * which we really should wait for to collect exit
1865 * status and code */
1866 return s
->main_pid
> 0;
1869 /* We don't know the pid */
1873 static int control_pid_good(Service
*s
) {
1876 return s
->control_pid
> 0;
1879 static int cgroup_good(Service
*s
) {
1884 if ((r
= cgroup_bonding_is_empty_list(UNIT(s
)->cgroup_bondings
)) < 0)
1890 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1894 if (f
!= SERVICE_SUCCESS
)
1897 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1899 if (allow_restart
&&
1900 !s
->forbid_restart
&&
1901 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1902 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& s
->result
== SERVICE_SUCCESS
) ||
1903 (s
->restart
== SERVICE_RESTART_ON_FAILURE
&& s
->result
!= SERVICE_SUCCESS
) ||
1904 (s
->restart
== SERVICE_RESTART_ON_ABORT
&& (s
->result
== SERVICE_FAILURE_SIGNAL
||
1905 s
->result
== SERVICE_FAILURE_CORE_DUMP
)))) {
1907 r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
);
1911 service_set_state(s
, SERVICE_AUTO_RESTART
);
1914 s
->forbid_restart
= false;
1919 log_warning("%s failed to run install restart timer: %s", UNIT(s
)->id
, strerror(-r
));
1920 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1923 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
1925 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1929 if (f
!= SERVICE_SUCCESS
)
1932 service_unwatch_control_pid(s
);
1934 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
])) {
1935 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1937 r
= service_spawn(s
,
1941 !s
->permissions_start_only
,
1942 !s
->root_directory_start_only
,
1951 service_set_state(s
, SERVICE_STOP_POST
);
1953 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1958 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s
)->id
, strerror(-r
));
1959 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1962 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1964 Set
*pid_set
= NULL
;
1965 bool wait_for_exit
= false;
1969 if (f
!= SERVICE_SUCCESS
)
1972 if (s
->kill_context
.kill_mode
!= KILL_NONE
) {
1973 int sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? s
->kill_context
.kill_signal
: SIGKILL
;
1975 if (s
->main_pid
> 0) {
1976 if (kill_and_sigcont(s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1977 log_warning("Failed to kill main process %li: %m", (long) s
->main_pid
);
1979 wait_for_exit
= !s
->main_pid_alien
;
1982 if (s
->control_pid
> 0) {
1983 if (kill_and_sigcont(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1984 log_warning("Failed to kill control process %li: %m", (long) s
->control_pid
);
1986 wait_for_exit
= true;
1989 if (s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
) {
1991 pid_set
= set_new(trivial_hash_func
, trivial_compare_func
);
1997 /* Exclude the main/control pids from being killed via the cgroup */
1998 if (s
->main_pid
> 0)
1999 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->main_pid
))) < 0)
2002 if (s
->control_pid
> 0)
2003 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0)
2006 r
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, sig
, true, false, pid_set
, NULL
);
2008 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
2009 log_warning("Failed to kill control group: %s", strerror(-r
));
2011 wait_for_exit
= true;
2018 if (wait_for_exit
) {
2019 if (s
->timeout_usec
> 0)
2020 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
2023 service_set_state(s
, state
);
2024 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
2025 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2027 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2032 log_warning("%s failed to kill processes: %s", UNIT(s
)->id
, strerror(-r
));
2034 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
2035 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
2037 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2043 static void service_enter_stop(Service
*s
, ServiceResult f
) {
2048 if (f
!= SERVICE_SUCCESS
)
2051 service_unwatch_control_pid(s
);
2053 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
])) {
2054 s
->control_command_id
= SERVICE_EXEC_STOP
;
2056 r
= service_spawn(s
,
2060 !s
->permissions_start_only
,
2061 !s
->root_directory_start_only
,
2069 service_set_state(s
, SERVICE_STOP
);
2071 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2076 log_warning("%s failed to run 'stop' task: %s", UNIT(s
)->id
, strerror(-r
));
2077 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2080 static void service_enter_running(Service
*s
, ServiceResult f
) {
2081 int main_pid_ok
, cgroup_ok
;
2084 if (f
!= SERVICE_SUCCESS
)
2087 main_pid_ok
= main_pid_good(s
);
2088 cgroup_ok
= cgroup_good(s
);
2090 if ((main_pid_ok
> 0 || (main_pid_ok
< 0 && cgroup_ok
!= 0)) &&
2091 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
))
2092 service_set_state(s
, SERVICE_RUNNING
);
2093 else if (s
->remain_after_exit
)
2094 service_set_state(s
, SERVICE_EXITED
);
2096 service_enter_stop(s
, SERVICE_SUCCESS
);
2099 static void service_enter_start_post(Service
*s
) {
2103 service_unwatch_control_pid(s
);
2105 if (s
->watchdog_usec
> 0)
2106 service_reset_watchdog(s
);
2108 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
])) {
2109 s
->control_command_id
= SERVICE_EXEC_START_POST
;
2111 r
= service_spawn(s
,
2115 !s
->permissions_start_only
,
2116 !s
->root_directory_start_only
,
2124 service_set_state(s
, SERVICE_START_POST
);
2126 service_enter_running(s
, SERVICE_SUCCESS
);
2131 log_warning("%s failed to run 'start-post' task: %s", UNIT(s
)->id
, strerror(-r
));
2132 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2135 static void service_enter_start(Service
*s
) {
2142 assert(s
->exec_command
[SERVICE_EXEC_START
]);
2143 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
|| s
->type
== SERVICE_ONESHOT
);
2145 if (s
->type
== SERVICE_FORKING
)
2146 service_unwatch_control_pid(s
);
2148 service_unwatch_main_pid(s
);
2150 /* We want to ensure that nobody leaks processes from
2151 * START_PRE here, so let's go on a killing spree, People
2152 * should not spawn long running processes from START_PRE. */
2153 cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, SIGKILL
, true, true, NULL
, "control");
2155 if (s
->type
== SERVICE_FORKING
) {
2156 s
->control_command_id
= SERVICE_EXEC_START
;
2157 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
2159 s
->main_command
= NULL
;
2161 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2162 s
->control_command
= NULL
;
2164 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
2167 r
= service_spawn(s
,
2169 s
->type
== SERVICE_FORKING
|| s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
|| s
->type
== SERVICE_ONESHOT
,
2174 s
->notify_access
!= NOTIFY_NONE
,
2180 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_IDLE
) {
2181 /* For simple services we immediately start
2182 * the START_POST binaries. */
2184 service_set_main_pid(s
, pid
);
2185 service_enter_start_post(s
);
2187 } else if (s
->type
== SERVICE_FORKING
) {
2189 /* For forking services we wait until the start
2190 * process exited. */
2192 s
->control_pid
= pid
;
2193 service_set_state(s
, SERVICE_START
);
2195 } else if (s
->type
== SERVICE_ONESHOT
||
2196 s
->type
== SERVICE_DBUS
||
2197 s
->type
== SERVICE_NOTIFY
) {
2199 /* For oneshot services we wait until the start
2200 * process exited, too, but it is our main process. */
2202 /* For D-Bus services we know the main pid right away,
2203 * but wait for the bus name to appear on the
2204 * bus. Notify services are similar. */
2206 service_set_main_pid(s
, pid
);
2207 service_set_state(s
, SERVICE_START
);
2209 assert_not_reached("Unknown service type");
2214 log_warning("%s failed to run 'start' task: %s", UNIT(s
)->id
, strerror(-r
));
2215 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2218 static void service_enter_start_pre(Service
*s
) {
2223 service_unwatch_control_pid(s
);
2225 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
])) {
2227 /* Before we start anything, let's clear up what might
2228 * be left from previous runs. */
2229 cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, SIGKILL
, true, true, NULL
, "control");
2231 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
2233 r
= service_spawn(s
,
2237 !s
->permissions_start_only
,
2238 !s
->root_directory_start_only
,
2246 service_set_state(s
, SERVICE_START_PRE
);
2248 service_enter_start(s
);
2253 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s
)->id
, strerror(-r
));
2254 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2257 static void service_enter_restart(Service
*s
) {
2262 dbus_error_init(&error
);
2264 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
2265 /* Don't restart things if we are going down anyway */
2266 log_info("Stop job pending for unit, delaying automatic restart.");
2268 r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
);
2275 /* Any units that are bound to this service must also be
2276 * restarted. We use JOB_RESTART (instead of the more obvious
2277 * JOB_START) here so that those dependency jobs will be added
2279 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
);
2283 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2284 * it will be canceled as part of the service_stop() call that
2285 * is executed as part of JOB_RESTART. */
2287 log_debug("%s scheduled restart job.", UNIT(s
)->id
);
2291 log_warning("%s failed to schedule restart job: %s", UNIT(s
)->id
, bus_error(&error
, -r
));
2292 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
2294 dbus_error_free(&error
);
2297 static void service_enter_reload(Service
*s
) {
2302 service_unwatch_control_pid(s
);
2304 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
])) {
2305 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
2307 r
= service_spawn(s
,
2311 !s
->permissions_start_only
,
2312 !s
->root_directory_start_only
,
2320 service_set_state(s
, SERVICE_RELOAD
);
2322 service_enter_running(s
, SERVICE_SUCCESS
);
2327 log_warning("%s failed to run 'reload' task: %s", UNIT(s
)->id
, strerror(-r
));
2328 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2329 service_enter_running(s
, SERVICE_SUCCESS
);
2332 static void service_run_next_control(Service
*s
) {
2336 assert(s
->control_command
);
2337 assert(s
->control_command
->command_next
);
2339 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
2341 s
->control_command
= s
->control_command
->command_next
;
2342 service_unwatch_control_pid(s
);
2344 r
= service_spawn(s
,
2348 !s
->permissions_start_only
,
2349 !s
->root_directory_start_only
,
2350 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
2351 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
2361 log_warning("%s failed to run next control task: %s", UNIT(s
)->id
, strerror(-r
));
2363 if (s
->state
== SERVICE_START_PRE
)
2364 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2365 else if (s
->state
== SERVICE_STOP
)
2366 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2367 else if (s
->state
== SERVICE_STOP_POST
)
2368 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
2369 else if (s
->state
== SERVICE_RELOAD
) {
2370 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
2371 service_enter_running(s
, SERVICE_SUCCESS
);
2373 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2376 static void service_run_next_main(Service
*s
) {
2381 assert(s
->main_command
);
2382 assert(s
->main_command
->command_next
);
2383 assert(s
->type
== SERVICE_ONESHOT
);
2385 s
->main_command
= s
->main_command
->command_next
;
2386 service_unwatch_main_pid(s
);
2388 r
= service_spawn(s
,
2395 s
->notify_access
!= NOTIFY_NONE
,
2401 service_set_main_pid(s
, pid
);
2406 log_warning("%s failed to run next main task: %s", UNIT(s
)->id
, strerror(-r
));
2407 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2410 static int service_start_limit_test(Service
*s
) {
2413 if (ratelimit_test(&s
->start_limit
))
2416 switch (s
->start_limit_action
) {
2418 case SERVICE_START_LIMIT_NONE
:
2419 log_warning("%s start request repeated too quickly, refusing to start.", UNIT(s
)->id
);
2422 case SERVICE_START_LIMIT_REBOOT
: {
2426 dbus_error_init(&error
);
2428 log_warning("%s start request repeated too quickly, rebooting.", UNIT(s
)->id
);
2430 r
= manager_add_job_by_name(UNIT(s
)->manager
, JOB_START
, SPECIAL_REBOOT_TARGET
, JOB_REPLACE
, true, &error
, NULL
);
2432 log_error("Failed to reboot: %s.", bus_error(&error
, r
));
2433 dbus_error_free(&error
);
2439 case SERVICE_START_LIMIT_REBOOT_FORCE
:
2440 log_warning("%s start request repeated too quickly, forcibly rebooting.", UNIT(s
)->id
);
2441 UNIT(s
)->manager
->exit_code
= MANAGER_REBOOT
;
2444 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE
:
2445 log_warning("%s start request repeated too quickly, rebooting immediately.", UNIT(s
)->id
);
2446 reboot(RB_AUTOBOOT
);
2450 log_error("start limit action=%i", s
->start_limit_action
);
2451 assert_not_reached("Unknown StartLimitAction.");
2457 static int service_start(Unit
*u
) {
2458 Service
*s
= SERVICE(u
);
2463 /* We cannot fulfill this request right now, try again later
2465 if (s
->state
== SERVICE_STOP
||
2466 s
->state
== SERVICE_STOP_SIGTERM
||
2467 s
->state
== SERVICE_STOP_SIGKILL
||
2468 s
->state
== SERVICE_STOP_POST
||
2469 s
->state
== SERVICE_FINAL_SIGTERM
||
2470 s
->state
== SERVICE_FINAL_SIGKILL
)
2473 /* Already on it! */
2474 if (s
->state
== SERVICE_START_PRE
||
2475 s
->state
== SERVICE_START
||
2476 s
->state
== SERVICE_START_POST
)
2479 /* A service that will be restarted must be stopped first to
2480 * trigger BindsTo and/or OnFailure dependencies. If a user
2481 * does not want to wait for the holdoff time to elapse, the
2482 * service should be manually restarted, not started. We
2483 * simply return EAGAIN here, so that any start jobs stay
2484 * queued, and assume that the auto restart timer will
2485 * eventually trigger the restart. */
2486 if (s
->state
== SERVICE_AUTO_RESTART
)
2489 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_FAILED
);
2491 /* Make sure we don't enter a busy loop of some kind. */
2492 r
= service_start_limit_test(s
);
2494 service_notify_sockets_dead(s
, true);
2498 s
->result
= SERVICE_SUCCESS
;
2499 s
->reload_result
= SERVICE_SUCCESS
;
2500 s
->main_pid_known
= false;
2501 s
->main_pid_alien
= false;
2502 s
->forbid_restart
= false;
2504 service_enter_start_pre(s
);
2508 static int service_stop(Unit
*u
) {
2509 Service
*s
= SERVICE(u
);
2513 /* Don't create restart jobs from here. */
2514 s
->forbid_restart
= true;
2517 if (s
->state
== SERVICE_STOP
||
2518 s
->state
== SERVICE_STOP_SIGTERM
||
2519 s
->state
== SERVICE_STOP_SIGKILL
||
2520 s
->state
== SERVICE_STOP_POST
||
2521 s
->state
== SERVICE_FINAL_SIGTERM
||
2522 s
->state
== SERVICE_FINAL_SIGKILL
)
2525 /* A restart will be scheduled or is in progress. */
2526 if (s
->state
== SERVICE_AUTO_RESTART
) {
2527 service_set_state(s
, SERVICE_DEAD
);
2531 /* If there's already something running we go directly into
2533 if (s
->state
== SERVICE_START_PRE
||
2534 s
->state
== SERVICE_START
||
2535 s
->state
== SERVICE_START_POST
||
2536 s
->state
== SERVICE_RELOAD
) {
2537 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2541 assert(s
->state
== SERVICE_RUNNING
||
2542 s
->state
== SERVICE_EXITED
);
2544 service_enter_stop(s
, SERVICE_SUCCESS
);
2548 static int service_reload(Unit
*u
) {
2549 Service
*s
= SERVICE(u
);
2553 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2555 service_enter_reload(s
);
2559 static bool service_can_reload(Unit
*u
) {
2560 Service
*s
= SERVICE(u
);
2564 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2567 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2568 Service
*s
= SERVICE(u
);
2574 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2575 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2576 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2578 if (s
->control_pid
> 0)
2579 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
2581 if (s
->main_pid_known
&& s
->main_pid
> 0)
2582 unit_serialize_item_format(u
, f
, "main-pid", "%lu", (unsigned long) s
->main_pid
);
2584 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2587 unit_serialize_item(u
, f
, "status-text", s
->status_text
);
2589 /* FIXME: There's a minor uncleanliness here: if there are
2590 * multiple commands attached here, we will start from the
2591 * first one again */
2592 if (s
->control_command_id
>= 0)
2593 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2595 if (s
->socket_fd
>= 0) {
2598 if ((copy
= fdset_put_dup(fds
, s
->socket_fd
)) < 0)
2601 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
2604 if (s
->main_exec_status
.pid
> 0) {
2605 unit_serialize_item_format(u
, f
, "main-exec-status-pid", "%lu", (unsigned long) s
->main_exec_status
.pid
);
2606 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2607 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2609 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2610 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2611 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2614 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
2615 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2620 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2621 Service
*s
= SERVICE(u
);
2628 if (streq(key
, "state")) {
2631 if ((state
= service_state_from_string(value
)) < 0)
2632 log_debug("Failed to parse state value %s", value
);
2634 s
->deserialized_state
= state
;
2635 } else if (streq(key
, "result")) {
2638 f
= service_result_from_string(value
);
2640 log_debug("Failed to parse result value %s", value
);
2641 else if (f
!= SERVICE_SUCCESS
)
2644 } else if (streq(key
, "reload-result")) {
2647 f
= service_result_from_string(value
);
2649 log_debug("Failed to parse reload result value %s", value
);
2650 else if (f
!= SERVICE_SUCCESS
)
2651 s
->reload_result
= f
;
2653 } else if (streq(key
, "control-pid")) {
2656 if (parse_pid(value
, &pid
) < 0)
2657 log_debug("Failed to parse control-pid value %s", value
);
2659 s
->control_pid
= pid
;
2660 } else if (streq(key
, "main-pid")) {
2663 if (parse_pid(value
, &pid
) < 0)
2664 log_debug("Failed to parse main-pid value %s", value
);
2666 service_set_main_pid(s
, (pid_t
) pid
);
2667 } else if (streq(key
, "main-pid-known")) {
2670 if ((b
= parse_boolean(value
)) < 0)
2671 log_debug("Failed to parse main-pid-known value %s", value
);
2673 s
->main_pid_known
= b
;
2674 } else if (streq(key
, "status-text")) {
2677 if ((t
= strdup(value
))) {
2678 free(s
->status_text
);
2682 } else if (streq(key
, "control-command")) {
2683 ServiceExecCommand id
;
2685 if ((id
= service_exec_command_from_string(value
)) < 0)
2686 log_debug("Failed to parse exec-command value %s", value
);
2688 s
->control_command_id
= id
;
2689 s
->control_command
= s
->exec_command
[id
];
2691 } else if (streq(key
, "socket-fd")) {
2694 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2695 log_debug("Failed to parse socket-fd value %s", value
);
2698 if (s
->socket_fd
>= 0)
2699 close_nointr_nofail(s
->socket_fd
);
2700 s
->socket_fd
= fdset_remove(fds
, fd
);
2702 } else if (streq(key
, "main-exec-status-pid")) {
2705 if (parse_pid(value
, &pid
) < 0)
2706 log_debug("Failed to parse main-exec-status-pid value %s", value
);
2708 s
->main_exec_status
.pid
= pid
;
2709 } else if (streq(key
, "main-exec-status-code")) {
2712 if (safe_atoi(value
, &i
) < 0)
2713 log_debug("Failed to parse main-exec-status-code value %s", value
);
2715 s
->main_exec_status
.code
= i
;
2716 } else if (streq(key
, "main-exec-status-status")) {
2719 if (safe_atoi(value
, &i
) < 0)
2720 log_debug("Failed to parse main-exec-status-status value %s", value
);
2722 s
->main_exec_status
.status
= i
;
2723 } else if (streq(key
, "main-exec-status-start"))
2724 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2725 else if (streq(key
, "main-exec-status-exit"))
2726 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2727 else if (streq(key
, "watchdog-timestamp"))
2728 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2730 log_debug("Unknown serialization key '%s'", key
);
2735 static UnitActiveState
service_active_state(Unit
*u
) {
2736 const UnitActiveState
*table
;
2740 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2742 return table
[SERVICE(u
)->state
];
2745 static const char *service_sub_state_to_string(Unit
*u
) {
2748 return service_state_to_string(SERVICE(u
)->state
);
2751 static bool service_check_gc(Unit
*u
) {
2752 Service
*s
= SERVICE(u
);
2756 /* Never clean up services that still have a process around,
2757 * even if the service is formally dead. */
2758 if (cgroup_good(s
) > 0 ||
2759 main_pid_good(s
) > 0 ||
2760 control_pid_good(s
) > 0)
2763 #ifdef HAVE_SYSV_COMPAT
2771 static bool service_check_snapshot(Unit
*u
) {
2772 Service
*s
= SERVICE(u
);
2776 return !s
->got_socket_fd
;
2779 static int service_retry_pid_file(Service
*s
) {
2782 assert(s
->pid_file
);
2783 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2785 r
= service_load_pid_file(s
, false);
2789 service_unwatch_pid_file(s
);
2791 service_enter_running(s
, SERVICE_SUCCESS
);
2795 static int service_watch_pid_file(Service
*s
) {
2798 log_debug("Setting watch for %s's PID file %s", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
2799 r
= path_spec_watch(s
->pid_file_pathspec
, UNIT(s
));
2803 /* the pidfile might have appeared just before we set the watch */
2804 service_retry_pid_file(s
);
2808 log_error("Failed to set a watch for %s's PID file %s: %s",
2809 UNIT(s
)->id
, s
->pid_file_pathspec
->path
, strerror(-r
));
2810 service_unwatch_pid_file(s
);
2814 static int service_demand_pid_file(Service
*s
) {
2817 assert(s
->pid_file
);
2818 assert(!s
->pid_file_pathspec
);
2820 ps
= new0(PathSpec
, 1);
2824 ps
->path
= strdup(s
->pid_file
);
2830 path_kill_slashes(ps
->path
);
2832 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2833 * keep their PID file open all the time. */
2834 ps
->type
= PATH_MODIFIED
;
2835 ps
->inotify_fd
= -1;
2837 s
->pid_file_pathspec
= ps
;
2839 return service_watch_pid_file(s
);
2842 static void service_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
2843 Service
*s
= SERVICE(u
);
2847 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2848 assert(s
->pid_file_pathspec
);
2849 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2851 log_debug("inotify event for %s", u
->id
);
2853 if (path_spec_fd_event(s
->pid_file_pathspec
, events
) < 0)
2856 if (service_retry_pid_file(s
) == 0)
2859 if (service_watch_pid_file(s
) < 0)
2864 service_unwatch_pid_file(s
);
2865 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2868 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2869 Service
*s
= SERVICE(u
);
2875 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
) : is_clean_exit_lsb(code
, 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_full(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2908 "%s: main process exited, code=%s, status=%i", u
->id
, sigchld_code_to_string(code
), status
);
2910 if (f
!= SERVICE_SUCCESS
)
2913 if (s
->main_command
&&
2914 s
->main_command
->command_next
&&
2915 f
== SERVICE_SUCCESS
) {
2917 /* There is another command to *
2918 * execute, so let's do that. */
2920 log_debug("%s running next main command for state %s", u
->id
, service_state_to_string(s
->state
));
2921 service_run_next_main(s
);
2925 /* The service exited, so the service is officially
2927 s
->main_command
= NULL
;
2931 case SERVICE_START_POST
:
2932 case SERVICE_RELOAD
:
2934 /* Need to wait until the operation is
2939 if (s
->type
== SERVICE_ONESHOT
) {
2940 /* This was our main goal, so let's go on */
2941 if (f
== SERVICE_SUCCESS
)
2942 service_enter_start_post(s
);
2944 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2947 assert(s
->type
== SERVICE_DBUS
|| s
->type
== SERVICE_NOTIFY
);
2952 case SERVICE_RUNNING
:
2953 service_enter_running(s
, f
);
2956 case SERVICE_STOP_SIGTERM
:
2957 case SERVICE_STOP_SIGKILL
:
2959 if (!control_pid_good(s
))
2960 service_enter_stop_post(s
, f
);
2962 /* If there is still a control process, wait for that first */
2966 assert_not_reached("Uh, main process died at wrong time.");
2970 } else if (s
->control_pid
== pid
) {
2974 if (s
->control_command
) {
2975 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2977 if (s
->control_command
->ignore
)
2978 f
= SERVICE_SUCCESS
;
2981 log_full(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2982 "%s: control process exited, code=%s status=%i", u
->id
, sigchld_code_to_string(code
), status
);
2984 if (f
!= SERVICE_SUCCESS
)
2987 /* Immediately get rid of the cgroup, so that the
2988 * kernel doesn't delay the cgroup empty messages for
2989 * the service cgroup any longer than necessary */
2990 cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, SIGKILL
, true, true, NULL
, "control");
2992 if (s
->control_command
&&
2993 s
->control_command
->command_next
&&
2994 f
== SERVICE_SUCCESS
) {
2996 /* There is another command to *
2997 * execute, so let's do that. */
2999 log_debug("%s running next control command for state %s", u
->id
, service_state_to_string(s
->state
));
3000 service_run_next_control(s
);
3003 /* No further commands for this step, so let's
3004 * figure out what to do next */
3006 s
->control_command
= NULL
;
3007 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
3009 log_debug("%s got final SIGCHLD for state %s", u
->id
, service_state_to_string(s
->state
));
3013 case SERVICE_START_PRE
:
3014 if (f
== SERVICE_SUCCESS
)
3015 service_enter_start(s
);
3017 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
3021 assert(s
->type
== SERVICE_FORKING
);
3023 if (f
!= SERVICE_SUCCESS
) {
3024 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
3029 bool has_start_post
;
3032 /* Let's try to load the pid file here if we can.
3033 * The PID file might actually be created by a START_POST
3034 * script. In that case don't worry if the loading fails. */
3036 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
3037 r
= service_load_pid_file(s
, !has_start_post
);
3038 if (!has_start_post
&& r
< 0) {
3039 r
= service_demand_pid_file(s
);
3040 if (r
< 0 || !cgroup_good(s
))
3041 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
3045 service_search_main_pid(s
);
3047 service_enter_start_post(s
);
3050 case SERVICE_START_POST
:
3051 if (f
!= SERVICE_SUCCESS
) {
3052 service_enter_stop(s
, f
);
3059 r
= service_load_pid_file(s
, true);
3061 r
= service_demand_pid_file(s
);
3062 if (r
< 0 || !cgroup_good(s
))
3063 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
3067 service_search_main_pid(s
);
3069 service_enter_running(s
, SERVICE_SUCCESS
);
3072 case SERVICE_RELOAD
:
3073 if (f
== SERVICE_SUCCESS
) {
3074 service_load_pid_file(s
, true);
3075 service_search_main_pid(s
);
3078 s
->reload_result
= f
;
3079 service_enter_running(s
, SERVICE_SUCCESS
);
3083 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
3086 case SERVICE_STOP_SIGTERM
:
3087 case SERVICE_STOP_SIGKILL
:
3088 if (main_pid_good(s
) <= 0)
3089 service_enter_stop_post(s
, f
);
3091 /* If there is still a service
3092 * process around, wait until
3093 * that one quit, too */
3096 case SERVICE_STOP_POST
:
3097 case SERVICE_FINAL_SIGTERM
:
3098 case SERVICE_FINAL_SIGKILL
:
3099 service_enter_dead(s
, f
, true);
3103 assert_not_reached("Uh, control process died at wrong time.");
3108 /* Notify clients about changed exit status */
3109 unit_add_to_dbus_queue(u
);
3112 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
3113 Service
*s
= SERVICE(u
);
3116 assert(elapsed
== 1);
3118 if (w
== &s
->watchdog_watch
) {
3119 service_handle_watchdog(s
);
3123 assert(w
== &s
->timer_watch
);
3127 case SERVICE_START_PRE
:
3129 log_warning("%s operation timed out. Terminating.", u
->id
);
3130 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3133 case SERVICE_START_POST
:
3134 log_warning("%s operation timed out. Stopping.", u
->id
);
3135 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
3138 case SERVICE_RELOAD
:
3139 log_warning("%s operation timed out. Stopping.", u
->id
);
3140 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
3141 service_enter_running(s
, SERVICE_SUCCESS
);
3145 log_warning("%s stopping timed out. Terminating.", u
->id
);
3146 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3149 case SERVICE_STOP_SIGTERM
:
3150 if (s
->kill_context
.send_sigkill
) {
3151 log_warning("%s stopping timed out. Killing.", u
->id
);
3152 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
3154 log_warning("%s stopping timed out. Skipping SIGKILL.", u
->id
);
3155 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
3160 case SERVICE_STOP_SIGKILL
:
3161 /* Uh, we sent a SIGKILL and it is still not gone?
3162 * Must be something we cannot kill, so let's just be
3163 * weirded out and continue */
3165 log_warning("%s still around after SIGKILL. Ignoring.", u
->id
);
3166 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
3169 case SERVICE_STOP_POST
:
3170 log_warning("%s stopping timed out (2). Terminating.", u
->id
);
3171 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
3174 case SERVICE_FINAL_SIGTERM
:
3175 if (s
->kill_context
.send_sigkill
) {
3176 log_warning("%s stopping timed out (2). Killing.", u
->id
);
3177 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
3179 log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u
->id
);
3180 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
3185 case SERVICE_FINAL_SIGKILL
:
3186 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u
->id
);
3187 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
3190 case SERVICE_AUTO_RESTART
:
3191 log_info("%s holdoff time over, scheduling restart.", u
->id
);
3192 service_enter_restart(s
);
3196 assert_not_reached("Timeout at wrong time.");
3200 static void service_cgroup_notify_event(Unit
*u
) {
3201 Service
*s
= SERVICE(u
);
3205 log_debug("%s: cgroup is empty", u
->id
);
3209 /* Waiting for SIGCHLD is usually more interesting,
3210 * because it includes return codes/signals. Which is
3211 * why we ignore the cgroup events for most cases,
3212 * except when we don't know pid which to expect the
3216 case SERVICE_START_POST
:
3217 /* If we were hoping for the daemon to write its PID file,
3218 * we can give up now. */
3219 if (s
->pid_file_pathspec
) {
3220 log_warning("%s never wrote its PID file. Failing.", UNIT(s
)->id
);
3221 service_unwatch_pid_file(s
);
3222 if (s
->state
== SERVICE_START
)
3223 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
3225 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
3229 case SERVICE_RUNNING
:
3230 /* service_enter_running() will figure out what to do */
3231 service_enter_running(s
, SERVICE_SUCCESS
);
3234 case SERVICE_STOP_SIGTERM
:
3235 case SERVICE_STOP_SIGKILL
:
3237 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
3238 service_enter_stop_post(s
, SERVICE_SUCCESS
);
3242 case SERVICE_FINAL_SIGTERM
:
3243 case SERVICE_FINAL_SIGKILL
:
3244 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
3245 service_enter_dead(s
, SERVICE_SUCCESS
, true);
3254 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
) {
3255 Service
*s
= SERVICE(u
);
3260 if (s
->notify_access
== NOTIFY_NONE
) {
3261 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3262 u
->id
, (unsigned long) pid
);
3266 if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
3267 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3268 u
->id
, (unsigned long) pid
, (unsigned long) s
->main_pid
);
3272 log_debug("%s: Got message", u
->id
);
3274 /* Interpret MAINPID= */
3275 if ((e
= strv_find_prefix(tags
, "MAINPID=")) &&
3276 (s
->state
== SERVICE_START
||
3277 s
->state
== SERVICE_START_POST
||
3278 s
->state
== SERVICE_RUNNING
||
3279 s
->state
== SERVICE_RELOAD
)) {
3281 if (parse_pid(e
+ 8, &pid
) < 0)
3282 log_warning("Failed to parse notification message %s", e
);
3284 log_debug("%s: got %s", u
->id
, e
);
3285 service_set_main_pid(s
, pid
);
3289 /* Interpret READY= */
3290 if (s
->type
== SERVICE_NOTIFY
&&
3291 s
->state
== SERVICE_START
&&
3292 strv_find(tags
, "READY=1")) {
3293 log_debug("%s: got READY=1", u
->id
);
3295 service_enter_start_post(s
);
3298 /* Interpret STATUS= */
3299 e
= strv_find_prefix(tags
, "STATUS=");
3305 if (!utf8_is_valid(e
+7)) {
3306 log_warning("Status message in notification is not UTF-8 clean.");
3312 log_error("Failed to allocate string.");
3316 log_debug("%s: got %s", u
->id
, e
);
3318 free(s
->status_text
);
3321 free(s
->status_text
);
3322 s
->status_text
= NULL
;
3326 if (strv_find(tags
, "WATCHDOG=1")) {
3327 log_debug("%s: got WATCHDOG=1", u
->id
);
3328 service_reset_watchdog(s
);
3331 /* Notify clients about changed status or main pid */
3332 unit_add_to_dbus_queue(u
);
3335 #ifdef HAVE_SYSV_COMPAT
3338 static void sysv_facility_in_insserv_conf(Manager
*mgr
) {
3342 if (!(f
= fopen("/etc/insserv.conf", "re"))) {
3343 r
= errno
== ENOENT
? 0 : -errno
;
3348 char l
[LINE_MAX
], *t
;
3349 char **parsed
= NULL
;
3351 if (!fgets(l
, sizeof(l
), f
)) {
3356 log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r
));
3361 if (*t
!= '$' && *t
!= '<')
3364 parsed
= strv_split(t
,WHITESPACE
);
3365 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3366 if (parsed
&& !startswith_no_case (parsed
[0], "<interactive>")) {
3369 if (sysv_translate_facility(parsed
[0], NULL
, &facility
) < 0)
3371 if ((u
= manager_get_unit(mgr
, facility
)) && (u
->type
== UNIT_TARGET
)) {
3373 char *dep
= NULL
, *name
, **j
;
3375 STRV_FOREACH (j
, parsed
+1) {
3384 if (sysv_translate_facility(name
, NULL
, &dep
) < 0)
3387 r
= unit_add_two_dependencies_by_name(u
, UNIT_BEFORE
, e
, dep
, NULL
, true);
3402 static int service_enumerate(Manager
*m
) {
3406 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
3407 Set
*runlevel_services
[ELEMENTSOF(rcnd_table
)], *shutdown_services
= NULL
;
3414 if (m
->running_as
!= MANAGER_SYSTEM
)
3417 zero(runlevel_services
);
3419 STRV_FOREACH(p
, m
->lookup_paths
.sysvrcnd_path
)
3420 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++) {
3424 path
= strjoin(*p
, "/", rcnd_table
[i
].path
, NULL
);
3433 if (!(d
= opendir(path
))) {
3434 if (errno
!= ENOENT
)
3435 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
3440 while ((de
= readdir(d
))) {
3443 if (ignore_file(de
->d_name
))
3446 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
3449 if (strlen(de
->d_name
) < 4)
3452 a
= undecchar(de
->d_name
[1]);
3453 b
= undecchar(de
->d_name
[2]);
3459 fpath
= strjoin(path
, "/", de
->d_name
, NULL
);
3465 if (access(fpath
, X_OK
) < 0) {
3467 if (errno
!= ENOENT
)
3468 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
3474 if (!(name
= sysv_translate_name(de
->d_name
+ 3))) {
3479 if ((r
= manager_load_unit_prepare(m
, name
, NULL
, NULL
, &service
)) < 0) {
3480 log_warning("Failed to prepare unit %s: %s", name
, strerror(-r
));
3484 if (de
->d_name
[0] == 'S') {
3486 if (rcnd_table
[i
].type
== RUNLEVEL_UP
|| rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
) {
3487 SERVICE(service
)->sysv_start_priority_from_rcnd
=
3488 MAX(a
*10 + b
, SERVICE(service
)->sysv_start_priority_from_rcnd
);
3490 SERVICE(service
)->sysv_enabled
= true;
3493 if ((r
= set_ensure_allocated(&runlevel_services
[i
], trivial_hash_func
, trivial_compare_func
)) < 0)
3496 if ((r
= set_put(runlevel_services
[i
], service
)) < 0)
3499 } else if (de
->d_name
[0] == 'K' &&
3500 (rcnd_table
[i
].type
== RUNLEVEL_DOWN
||
3501 rcnd_table
[i
].type
== RUNLEVEL_SYSINIT
)) {
3503 if ((r
= set_ensure_allocated(&shutdown_services
, trivial_hash_func
, trivial_compare_func
)) < 0)
3506 if ((r
= set_put(shutdown_services
, service
)) < 0)
3512 /* Now we loaded all stubs and are aware of the lowest
3513 start-up priority for all services, not let's actually load
3514 the services, this will also tell us which services are
3515 actually native now */
3516 manager_dispatch_load_queue(m
);
3518 /* If this is a native service, rely on native ways to pull in
3519 * a service, don't pull it in via sysv rcN.d links. */
3520 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
3521 SET_FOREACH(service
, runlevel_services
[i
], j
) {
3522 service
= unit_follow_merge(service
);
3524 if (service
->fragment_path
)
3527 if ((r
= unit_add_two_dependencies_by_name_inverse(service
, UNIT_AFTER
, UNIT_WANTS
, rcnd_table
[i
].target
, NULL
, true)) < 0)
3531 /* We honour K links only for halt/reboot. For the normal
3532 * runlevels we assume the stop jobs will be implicitly added
3533 * by the core logic. Also, we don't really distinguish here
3534 * between the runlevels 0 and 6 and just add them to the
3535 * special shutdown target. On SUSE the boot.d/ runlevel is
3536 * also used for shutdown, so we add links for that too to the
3537 * shutdown target.*/
3538 SET_FOREACH(service
, shutdown_services
, j
) {
3539 service
= unit_follow_merge(service
);
3541 if (service
->fragment_path
)
3544 if ((r
= unit_add_two_dependencies_by_name(service
, UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true)) < 0)
3551 sysv_facility_in_insserv_conf (m
);
3559 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
++)
3560 set_free(runlevel_services
[i
]);
3561 set_free(shutdown_services
);
3570 static void service_bus_name_owner_change(
3573 const char *old_owner
,
3574 const char *new_owner
) {
3576 Service
*s
= SERVICE(u
);
3581 assert(streq(s
->bus_name
, name
));
3582 assert(old_owner
|| new_owner
);
3584 if (old_owner
&& new_owner
)
3585 log_debug("%s's D-Bus name %s changed owner from %s to %s", u
->id
, name
, old_owner
, new_owner
);
3587 log_debug("%s's D-Bus name %s no longer registered by %s", u
->id
, name
, old_owner
);
3589 log_debug("%s's D-Bus name %s now registered by %s", u
->id
, name
, new_owner
);
3591 s
->bus_name_good
= !!new_owner
;
3593 if (s
->type
== SERVICE_DBUS
) {
3595 /* service_enter_running() will figure out what to
3597 if (s
->state
== SERVICE_RUNNING
)
3598 service_enter_running(s
, SERVICE_SUCCESS
);
3599 else if (s
->state
== SERVICE_START
&& new_owner
)
3600 service_enter_start_post(s
);
3602 } else if (new_owner
&&
3604 (s
->state
== SERVICE_START
||
3605 s
->state
== SERVICE_START_POST
||
3606 s
->state
== SERVICE_RUNNING
||
3607 s
->state
== SERVICE_RELOAD
)) {
3609 /* Try to acquire PID from bus service */
3610 log_debug("Trying to acquire PID from D-Bus name...");
3612 bus_query_pid(u
->manager
, name
);
3616 static void service_bus_query_pid_done(
3621 Service
*s
= SERVICE(u
);
3626 log_debug("%s's D-Bus name %s is now owned by process %u", u
->id
, name
, (unsigned) pid
);
3628 if (s
->main_pid
<= 0 &&
3629 (s
->state
== SERVICE_START
||
3630 s
->state
== SERVICE_START_POST
||
3631 s
->state
== SERVICE_RUNNING
||
3632 s
->state
== SERVICE_RELOAD
))
3633 service_set_main_pid(s
, pid
);
3636 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
) {
3641 /* This is called by the socket code when instantiating a new
3642 * service for a stream socket and the socket needs to be
3645 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3648 if (s
->socket_fd
>= 0)
3651 if (s
->state
!= SERVICE_DEAD
)
3655 s
->got_socket_fd
= true;
3657 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3659 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3662 static void service_reset_failed(Unit
*u
) {
3663 Service
*s
= SERVICE(u
);
3667 if (s
->state
== SERVICE_FAILED
)
3668 service_set_state(s
, SERVICE_DEAD
);
3670 s
->result
= SERVICE_SUCCESS
;
3671 s
->reload_result
= SERVICE_SUCCESS
;
3673 RATELIMIT_RESET(s
->start_limit
);
3676 static int service_kill(Unit
*u
, KillWho who
, KillMode mode
, int signo
, DBusError
*error
) {
3677 Service
*s
= SERVICE(u
);
3679 Set
*pid_set
= NULL
;
3683 if (s
->main_pid
<= 0 && who
== KILL_MAIN
) {
3684 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No main process to kill");
3688 if (s
->control_pid
<= 0 && who
== KILL_CONTROL
) {
3689 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No control process to kill");
3693 if (who
== KILL_CONTROL
|| who
== KILL_ALL
)
3694 if (s
->control_pid
> 0)
3695 if (kill(s
->control_pid
, signo
) < 0)
3698 if (who
== KILL_MAIN
|| who
== KILL_ALL
)
3699 if (s
->main_pid
> 0)
3700 if (kill(s
->main_pid
, signo
) < 0)
3703 if (who
== KILL_ALL
&& mode
== KILL_CONTROL_GROUP
) {
3706 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
)))
3709 /* Exclude the control/main pid from being killed via the cgroup */
3710 if (s
->control_pid
> 0)
3711 if ((q
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0) {
3716 if (s
->main_pid
> 0)
3717 if ((q
= set_put(pid_set
, LONG_TO_PTR(s
->main_pid
))) < 0) {
3721 q
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, signo
, false, false, pid_set
, NULL
);
3723 if (q
!= -EAGAIN
&& q
!= -ESRCH
&& q
!= -ENOENT
)
3734 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
3735 [SERVICE_DEAD
] = "dead",
3736 [SERVICE_START_PRE
] = "start-pre",
3737 [SERVICE_START
] = "start",
3738 [SERVICE_START_POST
] = "start-post",
3739 [SERVICE_RUNNING
] = "running",
3740 [SERVICE_EXITED
] = "exited",
3741 [SERVICE_RELOAD
] = "reload",
3742 [SERVICE_STOP
] = "stop",
3743 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
3744 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
3745 [SERVICE_STOP_POST
] = "stop-post",
3746 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
3747 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
3748 [SERVICE_FAILED
] = "failed",
3749 [SERVICE_AUTO_RESTART
] = "auto-restart",
3752 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
3754 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3755 [SERVICE_RESTART_NO
] = "no",
3756 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3757 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3758 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3759 [SERVICE_RESTART_ALWAYS
] = "always"
3762 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3764 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3765 [SERVICE_SIMPLE
] = "simple",
3766 [SERVICE_FORKING
] = "forking",
3767 [SERVICE_ONESHOT
] = "oneshot",
3768 [SERVICE_DBUS
] = "dbus",
3769 [SERVICE_NOTIFY
] = "notify",
3770 [SERVICE_IDLE
] = "idle"
3773 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3775 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3776 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3777 [SERVICE_EXEC_START
] = "ExecStart",
3778 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3779 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3780 [SERVICE_EXEC_STOP
] = "ExecStop",
3781 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3784 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3786 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3787 [NOTIFY_NONE
] = "none",
3788 [NOTIFY_MAIN
] = "main",
3789 [NOTIFY_ALL
] = "all"
3792 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3794 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3795 [SERVICE_SUCCESS
] = "success",
3796 [SERVICE_FAILURE_RESOURCES
] = "resources",
3797 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3798 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3799 [SERVICE_FAILURE_SIGNAL
] = "signal",
3800 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3801 [SERVICE_FAILURE_WATCHDOG
] = "watchdog"
3804 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3806 static const char* const start_limit_action_table
[_SERVICE_START_LIMIT_MAX
] = {
3807 [SERVICE_START_LIMIT_NONE
] = "none",
3808 [SERVICE_START_LIMIT_REBOOT
] = "reboot",
3809 [SERVICE_START_LIMIT_REBOOT_FORCE
] = "reboot-force",
3810 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE
] = "reboot-immediate"
3812 DEFINE_STRING_TABLE_LOOKUP(start_limit_action
, StartLimitAction
);
3814 const UnitVTable service_vtable
= {
3815 .object_size
= sizeof(Service
),
3821 .init
= service_init
,
3822 .done
= service_done
,
3823 .load
= service_load
,
3825 .coldplug
= service_coldplug
,
3827 .dump
= service_dump
,
3829 .start
= service_start
,
3830 .stop
= service_stop
,
3831 .reload
= service_reload
,
3833 .can_reload
= service_can_reload
,
3835 .kill
= service_kill
,
3837 .serialize
= service_serialize
,
3838 .deserialize_item
= service_deserialize_item
,
3840 .active_state
= service_active_state
,
3841 .sub_state_to_string
= service_sub_state_to_string
,
3843 .check_gc
= service_check_gc
,
3844 .check_snapshot
= service_check_snapshot
,
3846 .sigchld_event
= service_sigchld_event
,
3847 .timer_event
= service_timer_event
,
3848 .fd_event
= service_fd_event
,
3850 .reset_failed
= service_reset_failed
,
3852 .cgroup_notify_empty
= service_cgroup_notify_event
,
3853 .notify_message
= service_notify_message
,
3855 .bus_name_owner_change
= service_bus_name_owner_change
,
3856 .bus_query_pid_done
= service_bus_query_pid_done
,
3858 .bus_interface
= "org.freedesktop.systemd1.Service",
3859 .bus_message_handler
= bus_service_message_handler
,
3860 .bus_invalidating_properties
= bus_service_invalidating_properties
,
3862 #ifdef HAVE_SYSV_COMPAT
3863 .enumerate
= service_enumerate
,
3865 .status_message_formats
= {
3866 .starting_stopping
= {
3867 [0] = "Starting %s...",
3868 [1] = "Stopping %s...",
3870 .finished_start_job
= {
3871 [JOB_DONE
] = "Started %s.",
3872 [JOB_FAILED
] = "Failed to start %s.",
3873 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
3874 [JOB_TIMEOUT
] = "Timed out starting %s.",
3876 .finished_stop_job
= {
3877 [JOB_DONE
] = "Stopped %s.",
3878 [JOB_FAILED
] = "Stopped (with error) %s.",
3879 [JOB_TIMEOUT
] = "Timed out stopping %s.",