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
48 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
49 [SERVICE_DEAD
] = UNIT_INACTIVE
,
50 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
51 [SERVICE_START
] = UNIT_ACTIVATING
,
52 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
53 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
54 [SERVICE_RELOAD
] = UNIT_ACTIVE_RELOADING
,
55 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
56 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
57 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
58 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
59 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
60 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
61 [SERVICE_MAINTAINANCE
] = UNIT_INACTIVE
,
62 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
65 static void service_done(Unit
*u
) {
66 Service
*s
= SERVICE(u
);
76 exec_context_done(&s
->exec_context
);
77 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_MAX
);
78 s
->control_command
= NULL
;
80 /* This will leak a process, but at least no memory or any of
82 if (s
->main_pid
> 0) {
83 unit_unwatch_pid(u
, s
->main_pid
);
87 if (s
->control_pid
> 0) {
88 unit_unwatch_pid(u
, s
->control_pid
);
92 unit_unwatch_timer(u
, &s
->timer_watch
);
95 static int sysv_translate_name(const char *name
, char **_r
) {
97 static const char * const table
[] = {
98 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
99 "$network", SPECIAL_NETWORK_TARGET
,
100 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
101 "$portmap", SPECIAL_RPCBIND_TARGET
,
102 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
103 "$syslog", SPECIAL_SYSLOG_TARGET
,
104 "$time", SPECIAL_RTC_SET_TARGET
110 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
111 if (streq(table
[i
], name
)) {
112 if (!(r
= strdup(table
[i
+1])))
121 if (asprintf(&r
, "%s.service", name
) < 0)
132 static int sysv_chkconfig_order(Service
*s
) {
138 if (s
->sysv_start_priority
< 0)
141 /* For each pair of services where at least one lacks a LSB
142 * header, we use the start priority value to order things. */
144 LIST_FOREACH(units_per_type
, other
, UNIT(s
)->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
148 t
= (Service
*) other
;
153 if (t
->sysv_start_priority
< 0)
156 if (s
->sysv_has_lsb
&& t
->sysv_has_lsb
)
159 if (t
->sysv_start_priority
< s
->sysv_start_priority
)
161 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
166 /* FIXME: Maybe we should compare the name here lexicographically? */
168 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
))) < 0)
175 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
178 if (!(c
= new0(ExecCommand
, 1)))
181 if (!(c
->path
= strdup(path
))) {
186 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
195 static int sysv_exec_commands(Service
*s
) {
199 assert(s
->sysv_path
);
201 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
203 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
205 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
207 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
209 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
211 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
216 static int priority_from_rcd(Service
*s
, const char *init_script
) {
220 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvrcnd_path
)
221 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
226 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0)
234 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
239 while ((de
= readdir(d
))) {
242 if (ignore_file(de
->d_name
))
245 if (de
->d_name
[0] != 'S')
248 if (strlen(de
->d_name
) < 4)
251 if (!streq(de
->d_name
+ 3, init_script
))
254 /* Yay, we found it! Now decode the priority */
256 a
= undecchar(de
->d_name
[1]);
257 b
= undecchar(de
->d_name
[2]);
262 s
->sysv_start_priority
= a
*10 + b
;
264 log_debug("Determined priority %i from link farm for %s", s
->sysv_start_priority
, unit_id(UNIT(s
)));
276 static int service_load_sysv_path(Service
*s
, const char *path
, UnitLoadState
*new_state
) {
294 if (!(f
= fopen(path
, "re"))) {
295 r
= errno
== ENOENT
? 0 : -errno
;
299 s
->type
= SERVICE_FORKING
;
300 s
->restart
= SERVICE_ONCE
;
303 if (!(s
->sysv_path
= strdup(path
))) {
309 char l
[LINE_MAX
], *t
;
311 if (!fgets(l
, sizeof(l
), f
)) {
316 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
326 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
328 s
->sysv_has_lsb
= true;
332 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
338 t
+= strspn(t
, WHITESPACE
);
340 if (state
== NORMAL
) {
342 /* Try to parse Red Hat style chkconfig headers */
344 if (startswith(t
, "chkconfig:")) {
349 if (sscanf(t
+10, "%*15s %i %*i",
350 &start_priority
) != 1) {
352 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
356 if (start_priority
< 0 || start_priority
> 99) {
357 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
361 s
->sysv_start_priority
= start_priority
;
363 } else if (startswith(t
, "description:")) {
365 size_t k
= strlen(t
);
368 if (t
[k
-1] == '\\') {
373 if (!(d
= strdup(strstrip(t
+12)))) {
378 free(u
->meta
.description
);
379 u
->meta
.description
= d
;
381 } else if (startswith(t
, "pidfile:")) {
388 if (!path_is_absolute(fn
)) {
389 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
393 if (!(fn
= strdup(fn
))) {
402 } else if (state
== DESCRIPTION
) {
404 /* Try to parse Red Hat style description
407 size_t k
= strlen(t
);
415 assert(u
->meta
.description
);
416 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
421 free(u
->meta
.description
);
422 u
->meta
.description
= d
;
424 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
426 if (startswith(t
, "Provides:")) {
432 FOREACH_WORD(w
, z
, t
+9, i
) {
435 if (!(n
= strndup(w
, z
))) {
440 r
= sysv_translate_name(n
, &m
);
449 if (unit_name_to_type(m
) == UNIT_SERVICE
)
450 r
= unit_add_name(u
, m
);
452 if ((r
= unit_add_dependency_by_name_inverse(u
, UNIT_REQUIRES
, m
)) >= 0)
453 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
);
462 } else if (startswith(t
, "Required-Start:") ||
463 startswith(t
, "Should-Start:")) {
469 FOREACH_WORD(w
, z
, strchr(t
, ':')+1, i
) {
472 if (!(n
= strndup(w
, z
))) {
477 r
= sysv_translate_name(n
, &m
);
486 if (!(r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, m
)) < 0) {
491 r
= unit_add_dependency_by_name(
493 startswith(t
, "Required-Start:") ? UNIT_REQUIRES
: UNIT_WANTS
,
501 } else if (startswith(t
, "Description:")) {
504 state
= LSB_DESCRIPTION
;
506 if (!(d
= strdup(strstrip(t
+12)))) {
511 free(u
->meta
.description
);
512 u
->meta
.description
= d
;
514 } else if (startswith(t
, "Short-Description:") && !u
->meta
.description
) {
517 /* We use the short description only
518 * if no long description is set. */
522 if (!(d
= strdup(strstrip(t
+18)))) {
527 free(u
->meta
.description
);
528 u
->meta
.description
= d
;
530 } else if (state
== LSB_DESCRIPTION
) {
532 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
535 assert(u
->meta
.description
);
536 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
541 free(u
->meta
.description
);
542 u
->meta
.description
= d
;
549 /* If init scripts have no LSB header, then we enforce the
550 * ordering via the chkconfig priorities. We try to determine
551 * a priority for *all* init scripts here, since they are
552 * needed as soon as at least one non-LSB script is used. */
554 if (s
->sysv_start_priority
< 0) {
555 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", unit_id(u
));
557 if ((r
= priority_from_rcd(s
, file_name_from_path(path
))) < 0)
560 if (s
->sysv_start_priority
< 0)
561 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", unit_id(u
));
564 if ((r
= sysv_exec_commands(s
)) < 0)
567 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_BASIC_SERVICE
)) < 0 ||
568 (r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_BASIC_SERVICE
)) < 0)
571 *new_state
= UNIT_LOADED
;
582 static int service_load_sysv_name(Service
*s
, const char *name
, UnitLoadState
*new_state
) {
588 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvinit_path
) {
592 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
595 assert(endswith(path
, ".service"));
596 path
[strlen(path
)-8] = 0;
598 r
= service_load_sysv_path(s
, path
, new_state
);
604 if (*new_state
!= UNIT_STUB
)
611 static int service_load_sysv(Service
*s
, UnitLoadState
*new_state
) {
619 /* Load service data from SysV init scripts, preferably with
622 if (strv_isempty(UNIT(s
)->meta
.manager
->sysvinit_path
))
625 if ((t
= unit_id(UNIT(s
))))
626 if ((r
= service_load_sysv_name(s
, t
, new_state
)) < 0)
629 if (*new_state
== UNIT_STUB
)
630 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
631 if ((r
== service_load_sysv_name(s
, t
, new_state
)) < 0)
634 if (*new_state
!= UNIT_STUB
)
641 static int service_init(Unit
*u
, UnitLoadState
*new_state
) {
643 Service
*s
= SERVICE(u
);
647 assert(*new_state
== UNIT_STUB
);
649 /* First, reset everything to the defaults, in case this is a
655 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
656 s
->restart_usec
= DEFAULT_RESTART_USEC
;
658 exec_context_init(&s
->exec_context
);
660 s
->timer_watch
.type
= WATCH_INVALID
;
662 s
->state
= SERVICE_DEAD
;
664 s
->sysv_start_priority
= -1;
665 s
->permissions_start_only
= false;
666 s
->root_directory_start_only
= false;
668 s
->sysv_has_lsb
= false;
670 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
672 /* Load a .service file */
673 if ((r
= unit_load_fragment(u
, new_state
)) < 0)
676 /* Load a classic init script as a fallback, if we couldn't find anything */
677 if (*new_state
== UNIT_STUB
)
678 if ((r
= service_load_sysv(s
, new_state
)) < 0)
681 /* Still nothing found? Then let's give up */
682 if (*new_state
== UNIT_STUB
)
685 /* We were able to load something, then let's add in the
686 * dropin directories. */
687 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
690 /* This is a new unit? Then let's add in some extras */
691 if (*new_state
== UNIT_LOADED
) {
692 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
695 if ((r
= unit_add_default_cgroup(u
)) < 0)
698 if ((r
= sysv_chkconfig_order(s
)) < 0)
705 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
707 ServiceExecCommand c
;
708 Service
*s
= SERVICE(u
);
714 p2
= strappend(prefix
, "\t");
715 prefix2
= p2
? p2
: prefix
;
718 "%sService State: %s\n"
719 "%sPermissionsStartOnly: %s\n"
720 "%sRootDirectoryStartOnly: %s\n"
721 "%sValidNoProcess: %s\n"
723 prefix
, service_state_to_string(s
->state
),
724 prefix
, yes_no(s
->permissions_start_only
),
725 prefix
, yes_no(s
->root_directory_start_only
),
726 prefix
, yes_no(s
->valid_no_process
),
727 prefix
, service_type_to_string(s
->type
));
732 prefix
, s
->pid_file
);
734 exec_context_dump(&s
->exec_context
, f
, prefix
);
736 for (c
= 0; c
< _SERVICE_EXEC_MAX
; c
++) {
738 if (!s
->exec_command
[c
])
741 fprintf(f
, "%s→ %s:\n",
742 prefix
, service_exec_command_to_string(c
));
744 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
749 "%sSysV Init Script Path: %s\n"
750 "%sSysV Init Script has LSB Header: %s\n",
751 prefix
, s
->sysv_path
,
752 prefix
, yes_no(s
->sysv_has_lsb
));
754 if (s
->sysv_start_priority
>= 0)
756 "%sSysVStartPriority: %i\n",
757 prefix
, s
->sysv_start_priority
);
763 static int service_load_pid_file(Service
*s
) {
770 if (s
->main_pid_known
)
776 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
779 if ((r
= safe_atolu(k
, &p
)) < 0) {
784 if ((unsigned long) (pid_t
) p
!= p
)
788 s
->main_pid_known
= true;
793 static int service_get_sockets(Service
*s
, Set
**_set
) {
802 /* Collects all Socket objects that belong to this
803 * service. Note that a service might have multiple sockets
804 * via multiple names. */
806 if (!(set
= set_new(NULL
, NULL
)))
809 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
813 /* Look for all socket objects that go by any of our
814 * units and collect their fds */
816 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
821 p
= manager_get_unit(UNIT(s
)->meta
.manager
, k
);
826 if ((r
= set_put(set
, p
)) < 0)
839 static int service_notify_sockets(Service
*s
) {
847 /* Notifies all our sockets when we die */
849 if ((r
= service_get_sockets(s
, &set
)) < 0)
852 SET_FOREACH(sock
, set
, i
)
853 socket_notify_service_dead(sock
);
860 static void service_set_state(Service
*s
, ServiceState state
) {
861 ServiceState old_state
;
864 old_state
= s
->state
;
867 if (state
!= SERVICE_START_PRE
&&
868 state
!= SERVICE_START
&&
869 state
!= SERVICE_START_POST
&&
870 state
!= SERVICE_RELOAD
&&
871 state
!= SERVICE_STOP
&&
872 state
!= SERVICE_STOP_SIGTERM
&&
873 state
!= SERVICE_STOP_SIGKILL
&&
874 state
!= SERVICE_STOP_POST
&&
875 state
!= SERVICE_FINAL_SIGTERM
&&
876 state
!= SERVICE_FINAL_SIGKILL
&&
877 state
!= SERVICE_AUTO_RESTART
)
878 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
880 if (state
!= SERVICE_START
&&
881 state
!= SERVICE_START_POST
&&
882 state
!= SERVICE_RUNNING
&&
883 state
!= SERVICE_RELOAD
&&
884 state
!= SERVICE_STOP
&&
885 state
!= SERVICE_STOP_SIGTERM
&&
886 state
!= SERVICE_STOP_SIGKILL
)
887 if (s
->main_pid
> 0) {
888 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
892 if (state
!= SERVICE_START_PRE
&&
893 state
!= SERVICE_START
&&
894 state
!= SERVICE_START_POST
&&
895 state
!= SERVICE_RELOAD
&&
896 state
!= SERVICE_STOP
&&
897 state
!= SERVICE_STOP_SIGTERM
&&
898 state
!= SERVICE_STOP_SIGKILL
&&
899 state
!= SERVICE_STOP_POST
&&
900 state
!= SERVICE_FINAL_SIGTERM
&&
901 state
!= SERVICE_FINAL_SIGKILL
)
902 if (s
->control_pid
> 0) {
903 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
907 if (state
!= SERVICE_START_PRE
&&
908 state
!= SERVICE_START
&&
909 state
!= SERVICE_START_POST
&&
910 state
!= SERVICE_RELOAD
&&
911 state
!= SERVICE_STOP
&&
912 state
!= SERVICE_STOP_POST
)
913 s
->control_command
= NULL
;
915 if (state
== SERVICE_DEAD
||
916 state
== SERVICE_STOP
||
917 state
== SERVICE_STOP_SIGTERM
||
918 state
== SERVICE_STOP_SIGKILL
||
919 state
== SERVICE_STOP_POST
||
920 state
== SERVICE_FINAL_SIGTERM
||
921 state
== SERVICE_FINAL_SIGKILL
||
922 state
== SERVICE_MAINTAINANCE
||
923 state
== SERVICE_AUTO_RESTART
)
924 service_notify_sockets(s
);
926 log_debug("%s changed %s → %s", unit_id(UNIT(s
)), service_state_to_string(old_state
), service_state_to_string(state
));
928 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
931 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
943 if ((r
= service_get_sockets(s
, &set
)) < 0)
946 SET_FOREACH(sock
, set
, i
) {
950 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
962 if (!(t
= new(int, rn_fds
+cn_fds
))) {
968 memcpy(t
, rfds
, rn_fds
);
969 memcpy(t
+rn_fds
, cfds
, cn_fds
);
974 rn_fds
= rn_fds
+cn_fds
;
992 static int service_spawn(
997 bool apply_permissions
,
1011 if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
1015 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1018 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1020 if ((r
= exec_spawn(c
,
1025 UNIT(s
)->meta
.cgroup_bondings
,
1029 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1030 /* FIXME: we need to do something here */
1042 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1047 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1054 if (allow_restart
&&
1055 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1056 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1058 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1061 service_set_state(s
, SERVICE_AUTO_RESTART
);
1063 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
1068 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s
)), strerror(-r
));
1069 service_enter_dead(s
, false, false);
1072 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1074 static void service_enter_stop_post(Service
*s
, bool success
) {
1081 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
]))
1082 if ((r
= service_spawn(s
,
1086 !s
->permissions_start_only
,
1087 !s
->root_directory_start_only
,
1088 &s
->control_pid
)) < 0)
1092 service_set_state(s
, SERVICE_STOP_POST
);
1094 if (!s
->control_command
)
1095 service_enter_dead(s
, true, true);
1100 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1101 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1104 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1113 if (s
->main_pid
> 0 || s
->control_pid
> 0) {
1116 sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1119 if (s
->main_pid
> 0) {
1120 if (kill(s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1126 if (s
->control_pid
> 0) {
1127 if (kill(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1137 service_set_state(s
, state
);
1139 if (s
->main_pid
<= 0 && s
->control_pid
<= 0)
1140 service_enter_dead(s
, true, true);
1145 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s
)), strerror(-r
));
1149 service_set_state(s
, state
);
1150 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1151 service_enter_stop_post(s
, false);
1153 service_enter_dead(s
, false, true);
1156 static void service_enter_stop(Service
*s
, bool success
) {
1163 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
]))
1164 if ((r
= service_spawn(s
,
1168 !s
->permissions_start_only
,
1169 !s
->root_directory_start_only
,
1170 &s
->control_pid
)) < 0)
1173 service_set_state(s
, SERVICE_STOP
);
1175 if (!s
->control_command
)
1176 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1181 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1182 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1185 static void service_enter_start_post(Service
*s
) {
1189 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
]))
1190 if ((r
= service_spawn(s
,
1194 !s
->permissions_start_only
,
1195 !s
->root_directory_start_only
,
1196 &s
->control_pid
)) < 0)
1200 service_set_state(s
, SERVICE_START_POST
);
1202 if (!s
->control_command
)
1203 service_set_state(s
, SERVICE_RUNNING
);
1208 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1209 service_enter_stop(s
, false);
1212 static void service_enter_start(Service
*s
) {
1218 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1219 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1221 if ((r
= service_spawn(s
,
1222 s
->exec_command
[SERVICE_EXEC_START
],
1223 s
->type
== SERVICE_FORKING
,
1230 service_set_state(s
, SERVICE_START
);
1232 if (s
->type
== SERVICE_SIMPLE
) {
1233 /* For simple services we immediately start
1234 * the START_POST binaries. */
1237 s
->main_pid_known
= true;
1238 service_enter_start_post(s
);
1240 } else if (s
->type
== SERVICE_FORKING
) {
1242 /* For forking services we wait until the start
1243 * process exited. */
1245 s
->control_pid
= pid
;
1246 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1247 } else if (s
->type
== SERVICE_FINISH
) {
1249 /* For finishing services we wait until the start
1250 * process exited, too, but it is our main process. */
1253 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1255 assert_not_reached("Unknown service type");
1260 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s
)), strerror(-r
));
1261 service_enter_stop(s
, false);
1264 static void service_enter_start_pre(Service
*s
) {
1269 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
]))
1270 if ((r
= service_spawn(s
,
1274 !s
->permissions_start_only
,
1275 !s
->root_directory_start_only
,
1276 &s
->control_pid
)) < 0)
1279 service_set_state(s
, SERVICE_START_PRE
);
1281 if (!s
->control_command
)
1282 service_enter_start(s
);
1287 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1288 service_enter_dead(s
, false, true);
1291 static void service_enter_restart(Service
*s
) {
1295 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1298 log_debug("%s scheduled restart job.", unit_id(UNIT(s
)));
1299 service_enter_dead(s
, true, false);
1304 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s
)), strerror(-r
));
1305 service_enter_dead(s
, false, false);
1308 static void service_enter_reload(Service
*s
) {
1313 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
]))
1314 if ((r
= service_spawn(s
,
1318 !s
->permissions_start_only
,
1319 !s
->root_directory_start_only
,
1320 &s
->control_pid
)) < 0)
1323 service_set_state(s
, SERVICE_RELOAD
);
1325 if (!s
->control_command
)
1326 service_set_state(s
, SERVICE_RUNNING
);
1331 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1332 service_enter_stop(s
, false);
1335 static void service_run_next(Service
*s
, bool success
) {
1339 assert(s
->control_command
);
1340 assert(s
->control_command
->command_next
);
1345 s
->control_command
= s
->control_command
->command_next
;
1347 if ((r
= service_spawn(s
,
1351 !s
->permissions_start_only
,
1352 !s
->root_directory_start_only
,
1353 &s
->control_pid
)) < 0)
1359 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1361 if (s
->state
== SERVICE_STOP
)
1362 service_enter_stop_post(s
, false);
1363 else if (s
->state
== SERVICE_STOP_POST
)
1364 service_enter_dead(s
, false, true);
1366 service_enter_stop(s
, false);
1369 static int service_start(Unit
*u
) {
1370 Service
*s
= SERVICE(u
);
1374 /* We cannot fulfill this request right now, try again later
1376 if (s
->state
== SERVICE_STOP
||
1377 s
->state
== SERVICE_STOP_SIGTERM
||
1378 s
->state
== SERVICE_STOP_SIGKILL
||
1379 s
->state
== SERVICE_STOP_POST
||
1380 s
->state
== SERVICE_FINAL_SIGTERM
||
1381 s
->state
== SERVICE_FINAL_SIGKILL
)
1384 /* Already on it! */
1385 if (s
->state
== SERVICE_START_PRE
||
1386 s
->state
== SERVICE_START
||
1387 s
->state
== SERVICE_START_POST
)
1390 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1392 /* Make sure we don't enter a busy loop of some kind. */
1393 if (!ratelimit_test(&s
->ratelimit
)) {
1394 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u
));
1399 s
->main_pid_known
= false;
1401 service_enter_start_pre(s
);
1405 static int service_stop(Unit
*u
) {
1406 Service
*s
= SERVICE(u
);
1410 if (s
->state
== SERVICE_START_PRE
||
1411 s
->state
== SERVICE_START
||
1412 s
->state
== SERVICE_START_POST
||
1413 s
->state
== SERVICE_RELOAD
)
1416 if (s
->state
== SERVICE_AUTO_RESTART
) {
1417 service_set_state(s
, SERVICE_DEAD
);
1421 assert(s
->state
== SERVICE_RUNNING
);
1423 service_enter_stop(s
, true);
1427 static int service_reload(Unit
*u
) {
1428 Service
*s
= SERVICE(u
);
1432 assert(s
->state
== SERVICE_RUNNING
);
1434 service_enter_reload(s
);
1438 static bool service_can_reload(Unit
*u
) {
1439 Service
*s
= SERVICE(u
);
1443 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1446 static UnitActiveState
service_active_state(Unit
*u
) {
1449 return state_translation_table
[SERVICE(u
)->state
];
1452 static int main_pid_good(Service
*s
) {
1455 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1458 /* If we know the pid file, then lets just check if it is
1460 if (s
->main_pid_known
)
1461 return s
->main_pid
> 0;
1463 /* We don't know the pid */
1467 static bool control_pid_good(Service
*s
) {
1470 return s
->control_pid
> 0;
1473 static int cgroup_good(Service
*s
) {
1476 if (s
->valid_no_process
)
1479 return cgroup_bonding_is_empty_list(UNIT(s
)->meta
.cgroup_bondings
);
1482 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1483 Service
*s
= SERVICE(u
);
1489 success
= code
== CLD_EXITED
&& status
== 0;
1490 s
->failure
= s
->failure
|| !success
;
1492 if (s
->main_pid
== pid
) {
1494 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1497 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1498 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1499 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1502 log_debug("%s: main process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
1504 /* The service exited, so the service is officially
1509 case SERVICE_START_POST
:
1510 case SERVICE_RELOAD
:
1512 /* Need to wait until the operation is
1517 assert(s
->type
== SERVICE_FINISH
);
1519 /* This was our main goal, so let's go on */
1521 service_enter_start_post(s
);
1523 service_enter_stop(s
, false);
1526 case SERVICE_RUNNING
:
1527 service_enter_stop(s
, success
);
1530 case SERVICE_STOP_SIGTERM
:
1531 case SERVICE_STOP_SIGKILL
:
1533 if (!control_pid_good(s
))
1534 service_enter_stop_post(s
, success
);
1536 /* If there is still a control process, wait for that first */
1540 assert_not_reached("Uh, main process died at wrong time.");
1543 } else if (s
->control_pid
== pid
) {
1544 assert(s
->control_command
);
1546 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1549 log_debug("%s: control process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
1551 /* If we are shutting things down anyway we
1552 * don't care about failing commands. */
1554 if (s
->control_command
->command_next
&&
1555 (success
|| (s
->state
== SERVICE_STOP
|| s
->state
== SERVICE_STOP_POST
)))
1557 /* There is another command to *
1558 * execute, so let's do that. */
1560 service_run_next(s
, success
);
1563 /* No further commands for this step, so let's
1564 * figure out what to do next */
1566 log_debug("%s got final SIGCHLD for state %s", unit_id(u
), service_state_to_string(s
->state
));
1570 case SERVICE_START_PRE
:
1572 service_enter_start(s
);
1574 service_enter_stop(s
, false);
1578 assert(s
->type
== SERVICE_FORKING
);
1580 /* Let's try to load the pid
1581 * file here if we can. We
1582 * ignore the return value,
1583 * since the PID file might
1584 * actually be created by a
1585 * START_POST script */
1589 service_load_pid_file(s
);
1591 service_enter_start_post(s
);
1593 service_enter_stop(s
, false);
1597 case SERVICE_START_POST
:
1598 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
1601 /* Hmm, let's see if we can
1602 * load the pid now after the
1603 * start-post scripts got
1606 if ((r
= service_load_pid_file(s
)) < 0)
1607 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s
)), s
->pid_file
, strerror(-r
));
1612 case SERVICE_RELOAD
:
1614 if (main_pid_good(s
) != 0 && cgroup_good(s
) != 0)
1615 service_set_state(s
, SERVICE_RUNNING
);
1617 service_enter_stop(s
, true);
1619 service_enter_stop(s
, false);
1624 if (main_pid_good(s
) > 0)
1625 /* Still not dead and we know the PID? Let's go hunting. */
1626 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
1628 service_enter_stop_post(s
, success
);
1631 case SERVICE_STOP_SIGTERM
:
1632 case SERVICE_STOP_SIGKILL
:
1633 if (main_pid_good(s
) <= 0)
1634 service_enter_stop_post(s
, success
);
1636 /* If there is still a service
1637 * process around, wait until
1638 * that one quit, too */
1641 case SERVICE_STOP_POST
:
1642 case SERVICE_FINAL_SIGTERM
:
1643 case SERVICE_FINAL_SIGKILL
:
1644 service_enter_dead(s
, success
, true);
1648 assert_not_reached("Uh, control process died at wrong time.");
1652 assert_not_reached("Got SIGCHLD for unkown PID");
1655 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
1656 Service
*s
= SERVICE(u
);
1659 assert(elapsed
== 1);
1661 assert(w
== &s
->timer_watch
);
1665 case SERVICE_START_PRE
:
1667 case SERVICE_START_POST
:
1668 case SERVICE_RELOAD
:
1669 log_warning("%s operation timed out. Stopping.", unit_id(u
));
1670 service_enter_stop(s
, false);
1674 log_warning("%s stopping timed out. Terminating.", unit_id(u
));
1675 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1678 case SERVICE_STOP_SIGTERM
:
1679 log_warning("%s stopping timed out. Killing.", unit_id(u
));
1680 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
1683 case SERVICE_STOP_SIGKILL
:
1684 /* Uh, wie sent a SIGKILL and it is still not gone?
1685 * Must be something we cannot kill, so let's just be
1686 * weirded out and continue */
1688 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u
));
1689 service_enter_stop_post(s
, false);
1692 case SERVICE_STOP_POST
:
1693 log_warning("%s stopping timed out (2). Terminating.", unit_id(u
));
1694 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1697 case SERVICE_FINAL_SIGTERM
:
1698 log_warning("%s stopping timed out (2). Killing.", unit_id(u
));
1699 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
1702 case SERVICE_FINAL_SIGKILL
:
1703 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u
));
1704 service_enter_dead(s
, false, true);
1707 case SERVICE_AUTO_RESTART
:
1708 log_debug("%s holdoff time over, scheduling restart.", unit_id(u
));
1709 service_enter_restart(s
);
1713 assert_not_reached("Timeout at wrong time.");
1717 static void service_cgroup_notify_event(Unit
*u
) {
1718 Service
*s
= SERVICE(u
);
1722 log_debug("%s: cgroup is empty", unit_id(u
));
1726 /* Waiting for SIGCHLD is usually more interesting,
1727 * because it includes return codes/signals. Which is
1728 * why we ignore the cgroup events for most cases,
1729 * except when we don't know pid which to expect the
1732 case SERVICE_RUNNING
:
1734 if (!s
->valid_no_process
&& main_pid_good(s
) <= 0)
1735 service_enter_stop(s
, true);
1744 static int service_enumerate(Manager
*m
) {
1748 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
1753 STRV_FOREACH(p
, m
->sysvinit_path
)
1754 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
1759 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0) {
1767 if (!(d
= opendir(path
))) {
1768 if (errno
!= ENOENT
)
1769 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
1774 while ((de
= readdir(d
))) {
1775 Unit
*runlevel
, *service
;
1777 if (ignore_file(de
->d_name
))
1780 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
1783 if (strlen(de
->d_name
) < 4)
1788 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
], de
->d_name
) < 0) {
1793 if (access(fpath
, X_OK
) < 0) {
1795 if (errno
!= ENOENT
)
1796 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
1803 if (asprintf(&name
, "%s.service", de
->d_name
+3) < 0) {
1808 if ((r
= manager_load_unit(m
, name
, &service
)) < 0)
1811 if ((r
= manager_load_unit(m
, rcnd_table
[i
+1], &runlevel
)) < 0)
1814 if (de
->d_name
[0] == 'S') {
1815 if ((r
= unit_add_dependency(runlevel
, UNIT_WANTS
, service
)) < 0)
1818 if ((r
= unit_add_dependency(runlevel
, UNIT_AFTER
, service
)) < 0)
1821 } else if (de
->d_name
[0] == 'K' &&
1822 (streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL0_TARGET
) ||
1823 streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL6_TARGET
))) {
1825 /* We honour K links only for
1826 * halt/reboot. For the normal
1827 * runlevels we assume the
1829 * implicitly added by the
1832 if ((r
= unit_add_dependency(runlevel
, UNIT_CONFLICTS
, service
)) < 0)
1835 if ((r
= unit_add_dependency(runlevel
, UNIT_BEFORE
, service
)) < 0)
1852 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
1853 [SERVICE_DEAD
] = "dead",
1854 [SERVICE_START_PRE
] = "start-pre",
1855 [SERVICE_START
] = "start",
1856 [SERVICE_START_POST
] = "start-post",
1857 [SERVICE_RUNNING
] = "running",
1858 [SERVICE_RELOAD
] = "reload",
1859 [SERVICE_STOP
] = "stop",
1860 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
1861 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
1862 [SERVICE_STOP_POST
] = "stop-post",
1863 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
1864 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
1865 [SERVICE_MAINTAINANCE
] = "maintainance",
1866 [SERVICE_AUTO_RESTART
] = "auto-restart",
1869 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
1871 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
1872 [SERVICE_ONCE
] = "once",
1873 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
1874 [SERVICE_RESTART_ALWAYS
] = "restart-on-failure",
1877 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
1879 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
1880 [SERVICE_FORKING
] = "forking",
1881 [SERVICE_SIMPLE
] = "simple",
1882 [SERVICE_FINISH
] = "finish"
1885 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
1887 static const char* const service_exec_command_table
[_SERVICE_EXEC_MAX
] = {
1888 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
1889 [SERVICE_EXEC_START
] = "ExecStart",
1890 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
1891 [SERVICE_EXEC_RELOAD
] = "ExecReload",
1892 [SERVICE_EXEC_STOP
] = "ExecStop",
1893 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
1896 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
1898 const UnitVTable service_vtable
= {
1899 .suffix
= ".service",
1901 .init
= service_init
,
1902 .done
= service_done
,
1904 .dump
= service_dump
,
1906 .start
= service_start
,
1907 .stop
= service_stop
,
1908 .reload
= service_reload
,
1910 .can_reload
= service_can_reload
,
1912 .active_state
= service_active_state
,
1914 .sigchld_event
= service_sigchld_event
,
1915 .timer_event
= service_timer_event
,
1917 .cgroup_notify_empty
= service_cgroup_notify_event
,
1919 .enumerate
= service_enumerate