1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "load-fragment.h"
30 #include "load-dropin.h"
34 #define COMMENTS "#;\n"
35 #define NEWLINES "\n\r"
38 static const char * const rcnd_table
[] = {
39 "../rc0.d", SPECIAL_RUNLEVEL0_TARGET
,
40 "../rc1.d", SPECIAL_RUNLEVEL1_TARGET
,
41 "../rc2.d", SPECIAL_RUNLEVEL2_TARGET
,
42 "../rc3.d", SPECIAL_RUNLEVEL3_TARGET
,
43 "../rc4.d", SPECIAL_RUNLEVEL4_TARGET
,
44 "../rc5.d", SPECIAL_RUNLEVEL5_TARGET
,
45 "../rc6.d", SPECIAL_RUNLEVEL6_TARGET
49 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
50 [SERVICE_DEAD
] = UNIT_INACTIVE
,
51 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
52 [SERVICE_START
] = UNIT_ACTIVATING
,
53 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
54 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
55 [SERVICE_RELOAD
] = UNIT_ACTIVE_RELOADING
,
56 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
57 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
58 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
59 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
60 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
61 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
62 [SERVICE_MAINTAINANCE
] = UNIT_INACTIVE
,
63 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
66 static void service_done(Unit
*u
) {
67 Service
*s
= SERVICE(u
);
77 exec_context_done(&s
->exec_context
);
78 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_MAX
);
79 s
->control_command
= NULL
;
81 /* This will leak a process, but at least no memory or any of
83 if (s
->main_pid
> 0) {
84 unit_unwatch_pid(u
, s
->main_pid
);
88 if (s
->control_pid
> 0) {
89 unit_unwatch_pid(u
, s
->control_pid
);
93 unit_unwatch_timer(u
, &s
->timer_watch
);
96 static int sysv_translate_name(const char *name
, char **_r
) {
98 static const char * const table
[] = {
99 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
100 "$network", SPECIAL_NETWORK_TARGET
,
101 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
102 "$portmap", SPECIAL_RPCBIND_TARGET
,
103 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
104 "$syslog", SPECIAL_SYSLOG_TARGET
,
105 "$time", SPECIAL_RTC_SET_TARGET
111 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
112 if (streq(table
[i
], name
)) {
113 if (!(r
= strdup(table
[i
+1])))
122 if (asprintf(&r
, "%s.service", name
) < 0)
133 static int sysv_chkconfig_order(Service
*s
) {
139 if (s
->sysv_start_priority
< 0)
142 /* For each pair of services where at least one lacks a LSB
143 * header, we use the start priority value to order things. */
145 LIST_FOREACH(units_per_type
, other
, UNIT(s
)->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
149 t
= (Service
*) other
;
154 if (t
->sysv_start_priority
< 0)
157 if (s
->sysv_has_lsb
&& t
->sysv_has_lsb
)
160 if (t
->sysv_start_priority
< s
->sysv_start_priority
)
162 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
167 /* FIXME: Maybe we should compare the name here lexicographically? */
169 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
))) < 0)
176 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
179 if (!(c
= new0(ExecCommand
, 1)))
182 if (!(c
->path
= strdup(path
))) {
187 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
196 static int sysv_exec_commands(Service
*s
) {
200 assert(s
->sysv_path
);
202 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
204 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
206 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
208 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
210 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
212 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
217 static int priority_from_rcd(Service
*s
, const char *init_script
) {
221 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvinit_path
)
222 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
227 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0)
235 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
240 while ((de
= readdir(d
))) {
243 if (ignore_file(de
->d_name
))
246 if (de
->d_name
[0] != 'S')
249 if (strlen(de
->d_name
) < 4)
252 if (!streq(de
->d_name
+ 3, init_script
))
255 /* Yay, we found it! Now decode the priority */
257 a
= undecchar(de
->d_name
[1]);
258 b
= undecchar(de
->d_name
[2]);
263 s
->sysv_start_priority
= a
*10 + b
;
265 log_debug("Determined priority %i from link farm for %s", s
->sysv_start_priority
, unit_id(UNIT(s
)));
277 static int service_load_sysv_path(Service
*s
, const char *path
, UnitLoadState
*new_state
) {
295 if (!(f
= fopen(path
, "re"))) {
296 r
= errno
== ENOENT
? 0 : -errno
;
300 s
->type
= SERVICE_FORKING
;
301 s
->restart
= SERVICE_ONCE
;
304 if (!(s
->sysv_path
= strdup(path
))) {
310 char l
[LINE_MAX
], *t
;
312 if (!fgets(l
, sizeof(l
), f
)) {
317 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
327 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
329 s
->sysv_has_lsb
= true;
333 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
339 t
+= strspn(t
, WHITESPACE
);
341 if (state
== NORMAL
) {
343 /* Try to parse Red Hat style chkconfig headers */
345 if (startswith(t
, "chkconfig:")) {
350 if (sscanf(t
+10, "%*15s %i %*i",
351 &start_priority
) != 1) {
353 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
357 if (start_priority
< 0 || start_priority
> 99) {
358 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
362 s
->sysv_start_priority
= start_priority
;
364 } else if (startswith(t
, "description:")) {
366 size_t k
= strlen(t
);
369 if (t
[k
-1] == '\\') {
374 if (!(d
= strdup(strstrip(t
+12)))) {
379 free(u
->meta
.description
);
380 u
->meta
.description
= d
;
382 } else if (startswith(t
, "pidfile:")) {
389 if (!path_is_absolute(fn
)) {
390 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
394 if (!(fn
= strdup(fn
))) {
403 } else if (state
== DESCRIPTION
) {
405 /* Try to parse Red Hat style description
408 size_t k
= strlen(t
);
416 assert(u
->meta
.description
);
417 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
422 free(u
->meta
.description
);
423 u
->meta
.description
= d
;
425 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
427 if (startswith(t
, "Provides:")) {
433 FOREACH_WORD(w
, z
, t
+9, i
) {
436 if (!(n
= strndup(w
, z
))) {
441 r
= sysv_translate_name(n
, &m
);
450 if (unit_name_to_type(m
) == UNIT_SERVICE
)
451 r
= unit_add_name(u
, m
);
453 if ((r
= unit_add_dependency_by_name_inverse(u
, UNIT_REQUIRES
, m
)) >= 0)
454 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
);
463 } else if (startswith(t
, "Required-Start:") ||
464 startswith(t
, "Should-Start:")) {
470 FOREACH_WORD(w
, z
, strchr(t
, ':')+1, i
) {
473 if (!(n
= strndup(w
, z
))) {
478 r
= sysv_translate_name(n
, &m
);
487 if (!(r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, m
)) < 0) {
492 r
= unit_add_dependency_by_name(
494 startswith(t
, "Required-Start:") ? UNIT_REQUIRES
: UNIT_WANTS
,
502 } else if (startswith(t
, "Description:")) {
505 state
= LSB_DESCRIPTION
;
507 if (!(d
= strdup(strstrip(t
+12)))) {
512 free(u
->meta
.description
);
513 u
->meta
.description
= d
;
515 } else if (startswith(t
, "Short-Description:") && !u
->meta
.description
) {
518 /* We use the short description only
519 * if no long description is set. */
523 if (!(d
= strdup(strstrip(t
+18)))) {
528 free(u
->meta
.description
);
529 u
->meta
.description
= d
;
531 } else if (state
== LSB_DESCRIPTION
) {
533 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
536 assert(u
->meta
.description
);
537 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
542 free(u
->meta
.description
);
543 u
->meta
.description
= d
;
550 /* If init scripts have no LSB header, then we enforce the
551 * ordering via the chkconfig priorities. We try to determine
552 * a priority for *all* init scripts here, since they are
553 * needed as soon as at least one non-LSB script is used. */
555 if (s
->sysv_start_priority
< 0) {
556 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", unit_id(u
));
558 if ((r
= priority_from_rcd(s
, file_name_from_path(path
))) < 0)
561 if (s
->sysv_start_priority
< 0)
562 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", unit_id(u
));
565 if ((r
= sysv_exec_commands(s
)) < 0)
568 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_BASIC_SERVICE
)) < 0 ||
569 (r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_BASIC_SERVICE
)) < 0)
572 *new_state
= UNIT_LOADED
;
583 static int service_load_sysv_name(Service
*s
, const char *name
, UnitLoadState
*new_state
) {
589 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvinit_path
) {
593 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
596 assert(endswith(path
, ".service"));
597 path
[strlen(path
)-8] = 0;
599 r
= service_load_sysv_path(s
, path
, new_state
);
605 if (*new_state
!= UNIT_STUB
)
612 static int service_load_sysv(Service
*s
, UnitLoadState
*new_state
) {
620 /* Load service data from SysV init scripts, preferably with
623 if (strv_isempty(UNIT(s
)->meta
.manager
->sysvinit_path
))
626 if ((t
= unit_id(UNIT(s
))))
627 if ((r
= service_load_sysv_name(s
, t
, new_state
)) < 0)
630 if (*new_state
== UNIT_STUB
)
631 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
632 if ((r
== service_load_sysv_name(s
, t
, new_state
)) < 0)
635 if (*new_state
!= UNIT_STUB
)
642 static int service_init(Unit
*u
, UnitLoadState
*new_state
) {
644 Service
*s
= SERVICE(u
);
648 assert(*new_state
== UNIT_STUB
);
650 /* First, reset everything to the defaults, in case this is a
656 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
657 s
->restart_usec
= DEFAULT_RESTART_USEC
;
659 exec_context_init(&s
->exec_context
);
661 s
->timer_watch
.type
= WATCH_INVALID
;
663 s
->state
= SERVICE_DEAD
;
665 s
->sysv_start_priority
= -1;
666 s
->permissions_start_only
= false;
667 s
->root_directory_start_only
= false;
669 s
->sysv_has_lsb
= false;
671 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
673 /* Load a .service file */
674 if ((r
= unit_load_fragment(u
, new_state
)) < 0)
677 /* Load a classic init script as a fallback, if we couldn't find anything */
678 if (*new_state
== UNIT_STUB
)
679 if ((r
= service_load_sysv(s
, new_state
)) < 0)
682 /* Still nothing found? Then let's give up */
683 if (*new_state
== UNIT_STUB
)
686 /* We were able to load something, then let's add in the
687 * dropin directories. */
688 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
691 /* This is a new unit? Then let's add in some extras */
692 if (*new_state
== UNIT_LOADED
) {
693 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
696 if ((r
= unit_add_default_cgroup(u
)) < 0)
699 if ((r
= sysv_chkconfig_order(s
)) < 0)
706 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
708 ServiceExecCommand c
;
709 Service
*s
= SERVICE(u
);
715 p2
= strappend(prefix
, "\t");
716 prefix2
= p2
? p2
: prefix
;
719 "%sService State: %s\n"
720 "%sPermissionsStartOnly: %s\n"
721 "%sRootDirectoryStartOnly: %s\n"
722 "%sValidNoProcess: %s\n"
724 prefix
, service_state_to_string(s
->state
),
725 prefix
, yes_no(s
->permissions_start_only
),
726 prefix
, yes_no(s
->root_directory_start_only
),
727 prefix
, yes_no(s
->valid_no_process
),
728 prefix
, service_type_to_string(s
->type
));
733 prefix
, s
->pid_file
);
735 exec_context_dump(&s
->exec_context
, f
, prefix
);
737 for (c
= 0; c
< _SERVICE_EXEC_MAX
; c
++) {
739 if (!s
->exec_command
[c
])
742 fprintf(f
, "%s→ %s:\n",
743 prefix
, service_exec_command_to_string(c
));
745 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
750 "%sSysV Init Script Path: %s\n"
751 "%sSysV Init Script has LSB Header: %s\n",
752 prefix
, s
->sysv_path
,
753 prefix
, yes_no(s
->sysv_has_lsb
));
755 if (s
->sysv_start_priority
>= 0)
757 "%sSysVStartPriority: %i\n",
758 prefix
, s
->sysv_start_priority
);
764 static int service_load_pid_file(Service
*s
) {
771 if (s
->main_pid_known
)
777 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
780 if ((r
= safe_atolu(k
, &p
)) < 0) {
785 if ((unsigned long) (pid_t
) p
!= p
)
789 s
->main_pid_known
= true;
794 static int service_get_sockets(Service
*s
, Set
**_set
) {
803 /* Collects all Socket objects that belong to this
804 * service. Note that a service might have multiple sockets
805 * via multiple names. */
807 if (!(set
= set_new(NULL
, NULL
)))
810 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
814 /* Look for all socket objects that go by any of our
815 * units and collect their fds */
817 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
822 p
= manager_get_unit(UNIT(s
)->meta
.manager
, k
);
827 if ((r
= set_put(set
, p
)) < 0)
840 static int service_notify_sockets(Service
*s
) {
848 /* Notifies all our sockets when we die */
850 if ((r
= service_get_sockets(s
, &set
)) < 0)
853 SET_FOREACH(sock
, set
, i
)
854 socket_notify_service_dead(sock
);
861 static void service_set_state(Service
*s
, ServiceState state
) {
862 ServiceState old_state
;
865 old_state
= s
->state
;
868 if (state
!= SERVICE_START_PRE
&&
869 state
!= SERVICE_START
&&
870 state
!= SERVICE_START_POST
&&
871 state
!= SERVICE_RELOAD
&&
872 state
!= SERVICE_STOP
&&
873 state
!= SERVICE_STOP_SIGTERM
&&
874 state
!= SERVICE_STOP_SIGKILL
&&
875 state
!= SERVICE_STOP_POST
&&
876 state
!= SERVICE_FINAL_SIGTERM
&&
877 state
!= SERVICE_FINAL_SIGKILL
&&
878 state
!= SERVICE_AUTO_RESTART
)
879 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
881 if (state
!= SERVICE_START
&&
882 state
!= SERVICE_START_POST
&&
883 state
!= SERVICE_RUNNING
&&
884 state
!= SERVICE_RELOAD
&&
885 state
!= SERVICE_STOP
&&
886 state
!= SERVICE_STOP_SIGTERM
&&
887 state
!= SERVICE_STOP_SIGKILL
)
888 if (s
->main_pid
> 0) {
889 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
893 if (state
!= SERVICE_START_PRE
&&
894 state
!= SERVICE_START
&&
895 state
!= SERVICE_START_POST
&&
896 state
!= SERVICE_RELOAD
&&
897 state
!= SERVICE_STOP
&&
898 state
!= SERVICE_STOP_SIGTERM
&&
899 state
!= SERVICE_STOP_SIGKILL
&&
900 state
!= SERVICE_STOP_POST
&&
901 state
!= SERVICE_FINAL_SIGTERM
&&
902 state
!= SERVICE_FINAL_SIGKILL
)
903 if (s
->control_pid
> 0) {
904 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
908 if (state
!= SERVICE_START_PRE
&&
909 state
!= SERVICE_START
&&
910 state
!= SERVICE_START_POST
&&
911 state
!= SERVICE_RELOAD
&&
912 state
!= SERVICE_STOP
&&
913 state
!= SERVICE_STOP_POST
)
914 s
->control_command
= NULL
;
916 if (state
== SERVICE_DEAD
||
917 state
== SERVICE_STOP
||
918 state
== SERVICE_STOP_SIGTERM
||
919 state
== SERVICE_STOP_SIGKILL
||
920 state
== SERVICE_STOP_POST
||
921 state
== SERVICE_FINAL_SIGTERM
||
922 state
== SERVICE_FINAL_SIGKILL
||
923 state
== SERVICE_MAINTAINANCE
||
924 state
== SERVICE_AUTO_RESTART
)
925 service_notify_sockets(s
);
927 log_debug("%s changed %s → %s", unit_id(UNIT(s
)), service_state_to_string(old_state
), service_state_to_string(state
));
929 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
932 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
944 if ((r
= service_get_sockets(s
, &set
)) < 0)
947 SET_FOREACH(sock
, set
, i
) {
951 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
963 if (!(t
= new(int, rn_fds
+cn_fds
))) {
969 memcpy(t
, rfds
, rn_fds
);
970 memcpy(t
+rn_fds
, cfds
, cn_fds
);
975 rn_fds
= rn_fds
+cn_fds
;
993 static int service_spawn(
998 bool apply_permissions
,
1012 if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
1016 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1019 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1021 if ((r
= exec_spawn(c
,
1026 UNIT(s
)->meta
.cgroup_bondings
,
1030 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1031 /* FIXME: we need to do something here */
1043 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1048 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1055 if (allow_restart
&&
1056 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1057 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1059 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1062 service_set_state(s
, SERVICE_AUTO_RESTART
);
1064 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
1069 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s
)), strerror(-r
));
1070 service_enter_dead(s
, false, false);
1073 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1075 static void service_enter_stop_post(Service
*s
, bool success
) {
1082 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
]))
1083 if ((r
= service_spawn(s
,
1087 !s
->permissions_start_only
,
1088 !s
->root_directory_start_only
,
1089 &s
->control_pid
)) < 0)
1093 service_set_state(s
, SERVICE_STOP_POST
);
1095 if (!s
->control_command
)
1096 service_enter_dead(s
, true, true);
1101 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1102 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1105 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1114 if (s
->main_pid
> 0 || s
->control_pid
> 0) {
1117 sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1120 if (s
->main_pid
> 0) {
1121 if (kill(s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1127 if (s
->control_pid
> 0) {
1128 if (kill(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1138 service_set_state(s
, state
);
1140 if (s
->main_pid
<= 0 && s
->control_pid
<= 0)
1141 service_enter_dead(s
, true, true);
1146 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s
)), strerror(-r
));
1150 service_set_state(s
, state
);
1151 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1152 service_enter_stop_post(s
, false);
1154 service_enter_dead(s
, false, true);
1157 static void service_enter_stop(Service
*s
, bool success
) {
1164 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
]))
1165 if ((r
= service_spawn(s
,
1169 !s
->permissions_start_only
,
1170 !s
->root_directory_start_only
,
1171 &s
->control_pid
)) < 0)
1174 service_set_state(s
, SERVICE_STOP
);
1176 if (!s
->control_command
)
1177 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1182 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1183 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1186 static void service_enter_start_post(Service
*s
) {
1190 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
]))
1191 if ((r
= service_spawn(s
,
1195 !s
->permissions_start_only
,
1196 !s
->root_directory_start_only
,
1197 &s
->control_pid
)) < 0)
1201 service_set_state(s
, SERVICE_START_POST
);
1203 if (!s
->control_command
)
1204 service_set_state(s
, SERVICE_RUNNING
);
1209 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1210 service_enter_stop(s
, false);
1213 static void service_enter_start(Service
*s
) {
1219 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1220 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1222 if ((r
= service_spawn(s
,
1223 s
->exec_command
[SERVICE_EXEC_START
],
1224 s
->type
== SERVICE_FORKING
,
1231 service_set_state(s
, SERVICE_START
);
1233 if (s
->type
== SERVICE_SIMPLE
) {
1234 /* For simple services we immediately start
1235 * the START_POST binaries. */
1238 s
->main_pid_known
= true;
1239 service_enter_start_post(s
);
1241 } else if (s
->type
== SERVICE_FORKING
) {
1243 /* For forking services we wait until the start
1244 * process exited. */
1246 s
->control_pid
= pid
;
1247 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1248 } else if (s
->type
== SERVICE_FINISH
) {
1250 /* For finishing services we wait until the start
1251 * process exited, too, but it is our main process. */
1254 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1256 assert_not_reached("Unknown service type");
1261 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s
)), strerror(-r
));
1262 service_enter_stop(s
, false);
1265 static void service_enter_start_pre(Service
*s
) {
1270 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
]))
1271 if ((r
= service_spawn(s
,
1275 !s
->permissions_start_only
,
1276 !s
->root_directory_start_only
,
1277 &s
->control_pid
)) < 0)
1280 service_set_state(s
, SERVICE_START_PRE
);
1282 if (!s
->control_command
)
1283 service_enter_start(s
);
1288 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1289 service_enter_dead(s
, false, true);
1292 static void service_enter_restart(Service
*s
) {
1296 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1299 log_debug("%s scheduled restart job.", unit_id(UNIT(s
)));
1300 service_enter_dead(s
, true, false);
1305 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s
)), strerror(-r
));
1306 service_enter_dead(s
, false, false);
1309 static void service_enter_reload(Service
*s
) {
1314 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
]))
1315 if ((r
= service_spawn(s
,
1319 !s
->permissions_start_only
,
1320 !s
->root_directory_start_only
,
1321 &s
->control_pid
)) < 0)
1324 service_set_state(s
, SERVICE_RELOAD
);
1326 if (!s
->control_command
)
1327 service_set_state(s
, SERVICE_RUNNING
);
1332 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1333 service_enter_stop(s
, false);
1336 static void service_run_next(Service
*s
, bool success
) {
1340 assert(s
->control_command
);
1341 assert(s
->control_command
->command_next
);
1346 s
->control_command
= s
->control_command
->command_next
;
1348 if ((r
= service_spawn(s
,
1352 !s
->permissions_start_only
,
1353 !s
->root_directory_start_only
,
1354 &s
->control_pid
)) < 0)
1360 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1362 if (s
->state
== SERVICE_STOP
)
1363 service_enter_stop_post(s
, false);
1364 else if (s
->state
== SERVICE_STOP_POST
)
1365 service_enter_dead(s
, false, true);
1367 service_enter_stop(s
, false);
1370 static int service_start(Unit
*u
) {
1371 Service
*s
= SERVICE(u
);
1375 /* We cannot fulfill this request right now, try again later
1377 if (s
->state
== SERVICE_STOP
||
1378 s
->state
== SERVICE_STOP_SIGTERM
||
1379 s
->state
== SERVICE_STOP_SIGKILL
||
1380 s
->state
== SERVICE_STOP_POST
||
1381 s
->state
== SERVICE_FINAL_SIGTERM
||
1382 s
->state
== SERVICE_FINAL_SIGKILL
)
1385 /* Already on it! */
1386 if (s
->state
== SERVICE_START_PRE
||
1387 s
->state
== SERVICE_START
||
1388 s
->state
== SERVICE_START_POST
)
1391 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1393 /* Make sure we don't enter a busy loop of some kind. */
1394 if (!ratelimit_test(&s
->ratelimit
)) {
1395 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u
));
1400 s
->main_pid_known
= false;
1402 service_enter_start_pre(s
);
1406 static int service_stop(Unit
*u
) {
1407 Service
*s
= SERVICE(u
);
1411 if (s
->state
== SERVICE_START_PRE
||
1412 s
->state
== SERVICE_START
||
1413 s
->state
== SERVICE_START_POST
||
1414 s
->state
== SERVICE_RELOAD
)
1417 if (s
->state
== SERVICE_AUTO_RESTART
) {
1418 service_set_state(s
, SERVICE_DEAD
);
1422 assert(s
->state
== SERVICE_RUNNING
);
1424 service_enter_stop(s
, true);
1428 static int service_reload(Unit
*u
) {
1429 Service
*s
= SERVICE(u
);
1433 assert(s
->state
== SERVICE_RUNNING
);
1435 service_enter_reload(s
);
1439 static bool service_can_reload(Unit
*u
) {
1440 Service
*s
= SERVICE(u
);
1444 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1447 static UnitActiveState
service_active_state(Unit
*u
) {
1450 return state_translation_table
[SERVICE(u
)->state
];
1453 static int main_pid_good(Service
*s
) {
1456 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1459 /* If we know the pid file, then lets just check if it is
1461 if (s
->main_pid_known
)
1462 return s
->main_pid
> 0;
1464 /* We don't know the pid */
1468 static bool control_pid_good(Service
*s
) {
1471 return s
->control_pid
> 0;
1474 static int cgroup_good(Service
*s
) {
1477 if (s
->valid_no_process
)
1480 return cgroup_bonding_is_empty_list(UNIT(s
)->meta
.cgroup_bondings
);
1483 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1484 Service
*s
= SERVICE(u
);
1490 success
= code
== CLD_EXITED
&& status
== 0;
1491 s
->failure
= s
->failure
|| !success
;
1493 if (s
->main_pid
== pid
) {
1495 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1498 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1499 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1500 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1503 log_debug("%s: main process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
1505 /* The service exited, so the service is officially
1510 case SERVICE_START_POST
:
1511 case SERVICE_RELOAD
:
1513 /* Need to wait until the operation is
1518 assert(s
->type
== SERVICE_FINISH
);
1520 /* This was our main goal, so let's go on */
1522 service_enter_start_post(s
);
1524 service_enter_stop(s
, false);
1527 case SERVICE_RUNNING
:
1528 service_enter_stop(s
, success
);
1531 case SERVICE_STOP_SIGTERM
:
1532 case SERVICE_STOP_SIGKILL
:
1534 if (!control_pid_good(s
))
1535 service_enter_stop_post(s
, success
);
1537 /* If there is still a control process, wait for that first */
1541 assert_not_reached("Uh, main process died at wrong time.");
1544 } else if (s
->control_pid
== pid
) {
1545 assert(s
->control_command
);
1547 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1550 log_debug("%s: control process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
1552 /* If we are shutting things down anyway we
1553 * don't care about failing commands. */
1555 if (s
->control_command
->command_next
&&
1556 (success
|| (s
->state
== SERVICE_STOP
|| s
->state
== SERVICE_STOP_POST
)))
1558 /* There is another command to *
1559 * execute, so let's do that. */
1561 service_run_next(s
, success
);
1564 /* No further commands for this step, so let's
1565 * figure out what to do next */
1567 log_debug("%s got final SIGCHLD for state %s", unit_id(u
), service_state_to_string(s
->state
));
1571 case SERVICE_START_PRE
:
1573 service_enter_start(s
);
1575 service_enter_stop(s
, false);
1579 assert(s
->type
== SERVICE_FORKING
);
1581 /* Let's try to load the pid
1582 * file here if we can. We
1583 * ignore the return value,
1584 * since the PID file might
1585 * actually be created by a
1586 * START_POST script */
1590 service_load_pid_file(s
);
1592 service_enter_start_post(s
);
1594 service_enter_stop(s
, false);
1598 case SERVICE_START_POST
:
1599 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
1602 /* Hmm, let's see if we can
1603 * load the pid now after the
1604 * start-post scripts got
1607 if ((r
= service_load_pid_file(s
)) < 0)
1608 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s
)), s
->pid_file
, strerror(-r
));
1613 case SERVICE_RELOAD
:
1615 if (main_pid_good(s
) != 0 && cgroup_good(s
) != 0)
1616 service_set_state(s
, SERVICE_RUNNING
);
1618 service_enter_stop(s
, true);
1620 service_enter_stop(s
, false);
1625 if (main_pid_good(s
) > 0)
1626 /* Still not dead and we know the PID? Let's go hunting. */
1627 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
1629 service_enter_stop_post(s
, success
);
1632 case SERVICE_STOP_SIGTERM
:
1633 case SERVICE_STOP_SIGKILL
:
1634 if (main_pid_good(s
) <= 0)
1635 service_enter_stop_post(s
, success
);
1637 /* If there is still a service
1638 * process around, wait until
1639 * that one quit, too */
1642 case SERVICE_STOP_POST
:
1643 case SERVICE_FINAL_SIGTERM
:
1644 case SERVICE_FINAL_SIGKILL
:
1645 service_enter_dead(s
, success
, true);
1649 assert_not_reached("Uh, control process died at wrong time.");
1653 assert_not_reached("Got SIGCHLD for unkown PID");
1656 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
1657 Service
*s
= SERVICE(u
);
1660 assert(elapsed
== 1);
1662 assert(w
== &s
->timer_watch
);
1666 case SERVICE_START_PRE
:
1668 case SERVICE_START_POST
:
1669 case SERVICE_RELOAD
:
1670 log_warning("%s operation timed out. Stopping.", unit_id(u
));
1671 service_enter_stop(s
, false);
1675 log_warning("%s stopping timed out. Terminating.", unit_id(u
));
1676 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1679 case SERVICE_STOP_SIGTERM
:
1680 log_warning("%s stopping timed out. Killing.", unit_id(u
));
1681 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
1684 case SERVICE_STOP_SIGKILL
:
1685 /* Uh, wie sent a SIGKILL and it is still not gone?
1686 * Must be something we cannot kill, so let's just be
1687 * weirded out and continue */
1689 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u
));
1690 service_enter_stop_post(s
, false);
1693 case SERVICE_STOP_POST
:
1694 log_warning("%s stopping timed out (2). Terminating.", unit_id(u
));
1695 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1698 case SERVICE_FINAL_SIGTERM
:
1699 log_warning("%s stopping timed out (2). Killing.", unit_id(u
));
1700 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
1703 case SERVICE_FINAL_SIGKILL
:
1704 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u
));
1705 service_enter_dead(s
, false, true);
1708 case SERVICE_AUTO_RESTART
:
1709 log_debug("%s holdoff time over, scheduling restart.", unit_id(u
));
1710 service_enter_restart(s
);
1714 assert_not_reached("Timeout at wrong time.");
1718 static void service_cgroup_notify_event(Unit
*u
) {
1719 Service
*s
= SERVICE(u
);
1723 log_debug("%s: cgroup is empty", unit_id(u
));
1727 /* Waiting for SIGCHLD is usually more interesting,
1728 * because it includes return codes/signals. Which is
1729 * why we ignore the cgroup events for most cases,
1730 * except when we don't know pid which to expect the
1733 case SERVICE_RUNNING
:
1735 if (!s
->valid_no_process
&& main_pid_good(s
) <= 0)
1736 service_enter_stop(s
, true);
1745 static int service_enumerate(Manager
*m
) {
1749 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
1754 STRV_FOREACH(p
, m
->sysvinit_path
)
1755 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
1760 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0) {
1768 if (!(d
= opendir(path
))) {
1769 if (errno
!= ENOENT
)
1770 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
1775 while ((de
= readdir(d
))) {
1776 Unit
*runlevel
, *service
;
1778 if (ignore_file(de
->d_name
))
1781 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
1784 if (strlen(de
->d_name
) < 4)
1789 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
], de
->d_name
) < 0) {
1794 if (access(fpath
, X_OK
) < 0) {
1796 if (errno
!= ENOENT
)
1797 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
1804 if (asprintf(&name
, "%s.service", de
->d_name
+3) < 0) {
1809 if ((r
= manager_load_unit(m
, name
, &service
)) < 0)
1812 if ((r
= manager_load_unit(m
, rcnd_table
[i
+1], &runlevel
)) < 0)
1815 if (de
->d_name
[0] == 'S') {
1816 if ((r
= unit_add_dependency(runlevel
, UNIT_WANTS
, service
)) < 0)
1819 if ((r
= unit_add_dependency(runlevel
, UNIT_AFTER
, service
)) < 0)
1822 } else if (de
->d_name
[0] == 'K' &&
1823 (streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL0_TARGET
) ||
1824 streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL6_TARGET
))) {
1826 /* We honour K links only for
1827 * halt/reboot. For the normal
1828 * runlevels we assume the
1830 * implicitly added by the
1833 if ((r
= unit_add_dependency(runlevel
, UNIT_CONFLICTS
, service
)) < 0)
1836 if ((r
= unit_add_dependency(runlevel
, UNIT_BEFORE
, service
)) < 0)
1853 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
1854 [SERVICE_DEAD
] = "dead",
1855 [SERVICE_START_PRE
] = "start-pre",
1856 [SERVICE_START
] = "start",
1857 [SERVICE_START_POST
] = "start-post",
1858 [SERVICE_RUNNING
] = "running",
1859 [SERVICE_RELOAD
] = "reload",
1860 [SERVICE_STOP
] = "stop",
1861 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
1862 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
1863 [SERVICE_STOP_POST
] = "stop-post",
1864 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
1865 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
1866 [SERVICE_MAINTAINANCE
] = "maintainance",
1867 [SERVICE_AUTO_RESTART
] = "auto-restart",
1870 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
1872 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
1873 [SERVICE_ONCE
] = "once",
1874 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
1875 [SERVICE_RESTART_ALWAYS
] = "restart-on-failure",
1878 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
1880 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
1881 [SERVICE_FORKING
] = "forking",
1882 [SERVICE_SIMPLE
] = "simple",
1883 [SERVICE_FINISH
] = "finish"
1886 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
1888 static const char* const service_exec_command_table
[_SERVICE_EXEC_MAX
] = {
1889 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
1890 [SERVICE_EXEC_START
] = "ExecStart",
1891 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
1892 [SERVICE_EXEC_RELOAD
] = "ExecReload",
1893 [SERVICE_EXEC_STOP
] = "ExecStop",
1894 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
1897 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
1899 const UnitVTable service_vtable
= {
1900 .suffix
= ".service",
1902 .init
= service_init
,
1903 .done
= service_done
,
1905 .dump
= service_dump
,
1907 .start
= service_start
,
1908 .stop
= service_stop
,
1909 .reload
= service_reload
,
1911 .can_reload
= service_can_reload
,
1913 .active_state
= service_active_state
,
1915 .sigchld_event
= service_sigchld_event
,
1916 .timer_event
= service_timer_event
,
1918 .cgroup_notify_empty
= service_cgroup_notify_event
,
1920 .enumerate
= service_enumerate