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_SYSINIT_SERVICE
)) < 0 ||
569 (r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_SYSINIT_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
);
736 exec_context_dump(&s
->exec_context
, f
, prefix
);
738 for (c
= 0; c
< _SERVICE_EXEC_MAX
; c
++) {
740 if (!s
->exec_command
[c
])
743 fprintf(f
, "%s→ %s:\n",
744 prefix
, service_exec_command_to_string(c
));
746 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
751 "%sSysV Init Script Path: %s\n"
752 "%sSysV Init Script has LSB Header: %s\n",
753 prefix
, s
->sysv_path
,
754 prefix
, yes_no(s
->sysv_has_lsb
));
756 if (s
->sysv_start_priority
>= 0)
758 "%sSysVStartPriority: %i\n",
759 prefix
, s
->sysv_start_priority
);
765 static int service_load_pid_file(Service
*s
) {
772 if (s
->main_pid_known
)
778 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
781 if ((r
= safe_atolu(k
, &p
)) < 0) {
786 if ((unsigned long) (pid_t
) p
!= p
)
790 s
->main_pid_known
= true;
795 static int service_get_sockets(Service
*s
, Set
**_set
) {
804 /* Collects all Socket objects that belong to this
805 * service. Note that a service might have multiple sockets
806 * via multiple names. */
808 if (!(set
= set_new(NULL
, NULL
)))
811 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
815 /* Look for all socket objects that go by any of our
816 * units and collect their fds */
818 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
823 p
= manager_get_unit(UNIT(s
)->meta
.manager
, k
);
828 if ((r
= set_put(set
, p
)) < 0)
841 static int service_notify_sockets(Service
*s
) {
849 /* Notifies all our sockets when we die */
851 if ((r
= service_get_sockets(s
, &set
)) < 0)
854 SET_FOREACH(sock
, set
, i
)
855 socket_notify_service_dead(sock
);
862 static void service_set_state(Service
*s
, ServiceState state
) {
863 ServiceState old_state
;
866 old_state
= s
->state
;
869 if (state
!= SERVICE_START_PRE
&&
870 state
!= SERVICE_START
&&
871 state
!= SERVICE_START_POST
&&
872 state
!= SERVICE_RELOAD
&&
873 state
!= SERVICE_STOP
&&
874 state
!= SERVICE_STOP_SIGTERM
&&
875 state
!= SERVICE_STOP_SIGKILL
&&
876 state
!= SERVICE_STOP_POST
&&
877 state
!= SERVICE_FINAL_SIGTERM
&&
878 state
!= SERVICE_FINAL_SIGKILL
&&
879 state
!= SERVICE_AUTO_RESTART
)
880 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
882 if (state
!= SERVICE_START
&&
883 state
!= SERVICE_START_POST
&&
884 state
!= SERVICE_RUNNING
&&
885 state
!= SERVICE_RELOAD
&&
886 state
!= SERVICE_STOP
&&
887 state
!= SERVICE_STOP_SIGTERM
&&
888 state
!= SERVICE_STOP_SIGKILL
)
889 if (s
->main_pid
> 0) {
890 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
894 if (state
!= SERVICE_START_PRE
&&
895 state
!= SERVICE_START
&&
896 state
!= SERVICE_START_POST
&&
897 state
!= SERVICE_RELOAD
&&
898 state
!= SERVICE_STOP
&&
899 state
!= SERVICE_STOP_SIGTERM
&&
900 state
!= SERVICE_STOP_SIGKILL
&&
901 state
!= SERVICE_STOP_POST
&&
902 state
!= SERVICE_FINAL_SIGTERM
&&
903 state
!= SERVICE_FINAL_SIGKILL
)
904 if (s
->control_pid
> 0) {
905 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
909 if (state
!= SERVICE_START_PRE
&&
910 state
!= SERVICE_START
&&
911 state
!= SERVICE_START_POST
&&
912 state
!= SERVICE_RELOAD
&&
913 state
!= SERVICE_STOP
&&
914 state
!= SERVICE_STOP_POST
)
915 s
->control_command
= NULL
;
917 if (state
== SERVICE_DEAD
||
918 state
== SERVICE_STOP
||
919 state
== SERVICE_STOP_SIGTERM
||
920 state
== SERVICE_STOP_SIGKILL
||
921 state
== SERVICE_STOP_POST
||
922 state
== SERVICE_FINAL_SIGTERM
||
923 state
== SERVICE_FINAL_SIGKILL
||
924 state
== SERVICE_MAINTAINANCE
||
925 state
== SERVICE_AUTO_RESTART
)
926 service_notify_sockets(s
);
928 log_debug("%s changed %s → %s", unit_id(UNIT(s
)), service_state_to_string(old_state
), service_state_to_string(state
));
930 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
933 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
945 if ((r
= service_get_sockets(s
, &set
)) < 0)
948 SET_FOREACH(sock
, set
, i
) {
952 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
964 if (!(t
= new(int, rn_fds
+cn_fds
))) {
970 memcpy(t
, rfds
, rn_fds
);
971 memcpy(t
+rn_fds
, cfds
, cn_fds
);
976 rn_fds
= rn_fds
+cn_fds
;
994 static int service_spawn(
999 bool apply_permissions
,
1013 if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
1017 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1020 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1022 if ((r
= exec_spawn(c
,
1027 UNIT(s
)->meta
.cgroup_bondings
,
1031 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1032 /* FIXME: we need to do something here */
1044 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1049 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1056 if (allow_restart
&&
1057 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1058 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1060 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1063 service_set_state(s
, SERVICE_AUTO_RESTART
);
1065 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
1070 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s
)), strerror(-r
));
1071 service_enter_dead(s
, false, false);
1074 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1076 static void service_enter_stop_post(Service
*s
, bool success
) {
1083 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
]))
1084 if ((r
= service_spawn(s
,
1088 !s
->permissions_start_only
,
1089 !s
->root_directory_start_only
,
1090 &s
->control_pid
)) < 0)
1094 service_set_state(s
, SERVICE_STOP_POST
);
1096 if (!s
->control_command
)
1097 service_enter_dead(s
, true, true);
1102 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1103 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1106 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1115 if (s
->main_pid
> 0 || s
->control_pid
> 0) {
1118 sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1121 if (s
->main_pid
> 0) {
1122 if (kill(s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1128 if (s
->control_pid
> 0) {
1129 if (kill(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1139 service_set_state(s
, state
);
1141 if (s
->main_pid
<= 0 && s
->control_pid
<= 0)
1142 service_enter_dead(s
, true, true);
1147 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s
)), strerror(-r
));
1151 service_set_state(s
, state
);
1152 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1153 service_enter_stop_post(s
, false);
1155 service_enter_dead(s
, false, true);
1158 static void service_enter_stop(Service
*s
, bool success
) {
1165 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
]))
1166 if ((r
= service_spawn(s
,
1170 !s
->permissions_start_only
,
1171 !s
->root_directory_start_only
,
1172 &s
->control_pid
)) < 0)
1175 service_set_state(s
, SERVICE_STOP
);
1177 if (!s
->control_command
)
1178 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1183 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1184 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1187 static void service_enter_start_post(Service
*s
) {
1191 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
]))
1192 if ((r
= service_spawn(s
,
1196 !s
->permissions_start_only
,
1197 !s
->root_directory_start_only
,
1198 &s
->control_pid
)) < 0)
1202 service_set_state(s
, SERVICE_START_POST
);
1204 if (!s
->control_command
)
1205 service_set_state(s
, SERVICE_RUNNING
);
1210 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1211 service_enter_stop(s
, false);
1214 static void service_enter_start(Service
*s
) {
1220 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1221 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1223 if ((r
= service_spawn(s
,
1224 s
->exec_command
[SERVICE_EXEC_START
],
1225 s
->type
== SERVICE_FORKING
,
1232 service_set_state(s
, SERVICE_START
);
1234 if (s
->type
== SERVICE_SIMPLE
) {
1235 /* For simple services we immediately start
1236 * the START_POST binaries. */
1239 s
->main_pid_known
= true;
1240 service_enter_start_post(s
);
1242 } else if (s
->type
== SERVICE_FORKING
) {
1244 /* For forking services we wait until the start
1245 * process exited. */
1247 s
->control_pid
= pid
;
1248 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1249 } else if (s
->type
== SERVICE_FINISH
) {
1251 /* For finishing services we wait until the start
1252 * process exited, too, but it is our main process. */
1255 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1257 assert_not_reached("Unknown service type");
1262 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s
)), strerror(-r
));
1263 service_enter_stop(s
, false);
1266 static void service_enter_start_pre(Service
*s
) {
1271 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
]))
1272 if ((r
= service_spawn(s
,
1276 !s
->permissions_start_only
,
1277 !s
->root_directory_start_only
,
1278 &s
->control_pid
)) < 0)
1281 service_set_state(s
, SERVICE_START_PRE
);
1283 if (!s
->control_command
)
1284 service_enter_start(s
);
1289 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1290 service_enter_dead(s
, false, true);
1293 static void service_enter_restart(Service
*s
) {
1297 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1300 log_debug("%s scheduled restart job.", unit_id(UNIT(s
)));
1301 service_enter_dead(s
, true, false);
1306 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s
)), strerror(-r
));
1307 service_enter_dead(s
, false, false);
1310 static void service_enter_reload(Service
*s
) {
1315 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
]))
1316 if ((r
= service_spawn(s
,
1320 !s
->permissions_start_only
,
1321 !s
->root_directory_start_only
,
1322 &s
->control_pid
)) < 0)
1325 service_set_state(s
, SERVICE_RELOAD
);
1327 if (!s
->control_command
)
1328 service_set_state(s
, SERVICE_RUNNING
);
1333 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1334 service_enter_stop(s
, false);
1337 static void service_run_next(Service
*s
, bool success
) {
1341 assert(s
->control_command
);
1342 assert(s
->control_command
->command_next
);
1347 s
->control_command
= s
->control_command
->command_next
;
1349 if ((r
= service_spawn(s
,
1353 !s
->permissions_start_only
,
1354 !s
->root_directory_start_only
,
1355 &s
->control_pid
)) < 0)
1361 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1363 if (s
->state
== SERVICE_STOP
)
1364 service_enter_stop_post(s
, false);
1365 else if (s
->state
== SERVICE_STOP_POST
)
1366 service_enter_dead(s
, false, true);
1368 service_enter_stop(s
, false);
1371 static int service_start(Unit
*u
) {
1372 Service
*s
= SERVICE(u
);
1376 /* We cannot fulfill this request right now, try again later
1378 if (s
->state
== SERVICE_STOP
||
1379 s
->state
== SERVICE_STOP_SIGTERM
||
1380 s
->state
== SERVICE_STOP_SIGKILL
||
1381 s
->state
== SERVICE_STOP_POST
||
1382 s
->state
== SERVICE_FINAL_SIGTERM
||
1383 s
->state
== SERVICE_FINAL_SIGKILL
)
1386 /* Already on it! */
1387 if (s
->state
== SERVICE_START_PRE
||
1388 s
->state
== SERVICE_START
||
1389 s
->state
== SERVICE_START_POST
)
1392 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1394 /* Make sure we don't enter a busy loop of some kind. */
1395 if (!ratelimit_test(&s
->ratelimit
)) {
1396 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u
));
1401 s
->main_pid_known
= false;
1403 service_enter_start_pre(s
);
1407 static int service_stop(Unit
*u
) {
1408 Service
*s
= SERVICE(u
);
1412 if (s
->state
== SERVICE_START_PRE
||
1413 s
->state
== SERVICE_START
||
1414 s
->state
== SERVICE_START_POST
||
1415 s
->state
== SERVICE_RELOAD
)
1418 if (s
->state
== SERVICE_AUTO_RESTART
) {
1419 service_set_state(s
, SERVICE_DEAD
);
1423 assert(s
->state
== SERVICE_RUNNING
);
1425 service_enter_stop(s
, true);
1429 static int service_reload(Unit
*u
) {
1430 Service
*s
= SERVICE(u
);
1434 assert(s
->state
== SERVICE_RUNNING
);
1436 service_enter_reload(s
);
1440 static bool service_can_reload(Unit
*u
) {
1441 Service
*s
= SERVICE(u
);
1445 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1448 static UnitActiveState
service_active_state(Unit
*u
) {
1451 return state_translation_table
[SERVICE(u
)->state
];
1454 static int main_pid_good(Service
*s
) {
1457 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1460 /* If we know the pid file, then lets just check if it is
1462 if (s
->main_pid_known
)
1463 return s
->main_pid
> 0;
1465 /* We don't know the pid */
1469 static bool control_pid_good(Service
*s
) {
1472 return s
->control_pid
> 0;
1475 static int cgroup_good(Service
*s
) {
1478 if (s
->valid_no_process
)
1481 return cgroup_bonding_is_empty_list(UNIT(s
)->meta
.cgroup_bondings
);
1484 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1485 Service
*s
= SERVICE(u
);
1491 success
= code
== CLD_EXITED
&& status
== 0;
1492 s
->failure
= s
->failure
|| !success
;
1494 if (s
->main_pid
== pid
) {
1496 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1499 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1500 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1501 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1504 log_debug("%s: main process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
1506 /* The service exited, so the service is officially
1511 case SERVICE_START_POST
:
1512 case SERVICE_RELOAD
:
1514 /* Need to wait until the operation is
1519 assert(s
->type
== SERVICE_FINISH
);
1521 /* This was our main goal, so let's go on */
1523 service_enter_start_post(s
);
1525 service_enter_stop(s
, false);
1528 case SERVICE_RUNNING
:
1529 service_enter_stop(s
, success
);
1532 case SERVICE_STOP_SIGTERM
:
1533 case SERVICE_STOP_SIGKILL
:
1535 if (!control_pid_good(s
))
1536 service_enter_stop_post(s
, success
);
1538 /* If there is still a control process, wait for that first */
1542 assert_not_reached("Uh, main process died at wrong time.");
1545 } else if (s
->control_pid
== pid
) {
1546 assert(s
->control_command
);
1548 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1551 log_debug("%s: control process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
1553 /* If we are shutting things down anyway we
1554 * don't care about failing commands. */
1556 if (s
->control_command
->command_next
&&
1557 (success
|| (s
->state
== SERVICE_STOP
|| s
->state
== SERVICE_STOP_POST
)))
1559 /* There is another command to *
1560 * execute, so let's do that. */
1562 service_run_next(s
, success
);
1565 /* No further commands for this step, so let's
1566 * figure out what to do next */
1568 log_debug("%s got final SIGCHLD for state %s", unit_id(u
), service_state_to_string(s
->state
));
1572 case SERVICE_START_PRE
:
1574 service_enter_start(s
);
1576 service_enter_stop(s
, false);
1580 assert(s
->type
== SERVICE_FORKING
);
1582 /* Let's try to load the pid
1583 * file here if we can. We
1584 * ignore the return value,
1585 * since the PID file might
1586 * actually be created by a
1587 * START_POST script */
1591 service_load_pid_file(s
);
1593 service_enter_start_post(s
);
1595 service_enter_stop(s
, false);
1599 case SERVICE_START_POST
:
1600 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
1603 /* Hmm, let's see if we can
1604 * load the pid now after the
1605 * start-post scripts got
1608 if ((r
= service_load_pid_file(s
)) < 0)
1609 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s
)), s
->pid_file
, strerror(-r
));
1614 case SERVICE_RELOAD
:
1616 if (main_pid_good(s
) != 0 && cgroup_good(s
) != 0)
1617 service_set_state(s
, SERVICE_RUNNING
);
1619 service_enter_stop(s
, true);
1621 service_enter_stop(s
, false);
1626 if (main_pid_good(s
) > 0)
1627 /* Still not dead and we know the PID? Let's go hunting. */
1628 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
1630 service_enter_stop_post(s
, success
);
1633 case SERVICE_STOP_SIGTERM
:
1634 case SERVICE_STOP_SIGKILL
:
1635 if (main_pid_good(s
) <= 0)
1636 service_enter_stop_post(s
, success
);
1638 /* If there is still a service
1639 * process around, wait until
1640 * that one quit, too */
1643 case SERVICE_STOP_POST
:
1644 case SERVICE_FINAL_SIGTERM
:
1645 case SERVICE_FINAL_SIGKILL
:
1646 service_enter_dead(s
, success
, true);
1650 assert_not_reached("Uh, control process died at wrong time.");
1654 assert_not_reached("Got SIGCHLD for unkown PID");
1657 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
1658 Service
*s
= SERVICE(u
);
1661 assert(elapsed
== 1);
1663 assert(w
== &s
->timer_watch
);
1667 case SERVICE_START_PRE
:
1669 case SERVICE_START_POST
:
1670 case SERVICE_RELOAD
:
1671 log_warning("%s operation timed out. Stopping.", unit_id(u
));
1672 service_enter_stop(s
, false);
1676 log_warning("%s stopping timed out. Terminating.", unit_id(u
));
1677 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1680 case SERVICE_STOP_SIGTERM
:
1681 log_warning("%s stopping timed out. Killing.", unit_id(u
));
1682 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
1685 case SERVICE_STOP_SIGKILL
:
1686 /* Uh, wie sent a SIGKILL and it is still not gone?
1687 * Must be something we cannot kill, so let's just be
1688 * weirded out and continue */
1690 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u
));
1691 service_enter_stop_post(s
, false);
1694 case SERVICE_STOP_POST
:
1695 log_warning("%s stopping timed out (2). Terminating.", unit_id(u
));
1696 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1699 case SERVICE_FINAL_SIGTERM
:
1700 log_warning("%s stopping timed out (2). Killing.", unit_id(u
));
1701 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
1704 case SERVICE_FINAL_SIGKILL
:
1705 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u
));
1706 service_enter_dead(s
, false, true);
1709 case SERVICE_AUTO_RESTART
:
1710 log_debug("%s holdoff time over, scheduling restart.", unit_id(u
));
1711 service_enter_restart(s
);
1715 assert_not_reached("Timeout at wrong time.");
1719 static void service_cgroup_notify_event(Unit
*u
) {
1720 Service
*s
= SERVICE(u
);
1724 log_debug("%s: cgroup is empty", unit_id(u
));
1728 /* Waiting for SIGCHLD is usually more interesting,
1729 * because it includes return codes/signals. Which is
1730 * why we ignore the cgroup events for most cases,
1731 * except when we don't know pid which to expect the
1734 case SERVICE_RUNNING
:
1736 if (!s
->valid_no_process
&& main_pid_good(s
) <= 0)
1737 service_enter_stop(s
, true);
1746 static int service_enumerate(Manager
*m
) {
1750 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
1755 STRV_FOREACH(p
, m
->sysvinit_path
)
1756 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
1761 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0) {
1769 if (!(d
= opendir(path
))) {
1770 if (errno
!= ENOENT
)
1771 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
1776 while ((de
= readdir(d
))) {
1777 Unit
*runlevel
, *service
;
1779 if (ignore_file(de
->d_name
))
1782 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
1785 if (strlen(de
->d_name
) < 4)
1790 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
], de
->d_name
) < 0) {
1795 if (access(fpath
, X_OK
) < 0) {
1797 if (errno
!= ENOENT
)
1798 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
1805 if (asprintf(&name
, "%s.service", de
->d_name
+3) < 0) {
1810 if ((r
= manager_load_unit(m
, name
, &service
)) < 0)
1813 if ((r
= manager_load_unit(m
, rcnd_table
[i
+1], &runlevel
)) < 0)
1816 if (de
->d_name
[0] == 'S') {
1817 if ((r
= unit_add_dependency(runlevel
, UNIT_WANTS
, service
)) < 0)
1820 if ((r
= unit_add_dependency(runlevel
, UNIT_AFTER
, service
)) < 0)
1823 } else if (de
->d_name
[0] == 'K' &&
1824 (streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL0_TARGET
) ||
1825 streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL6_TARGET
))) {
1827 /* We honour K links only for
1828 * halt/reboot. For the normal
1829 * runlevels we assume the
1831 * implicitly added by the
1834 if ((r
= unit_add_dependency(runlevel
, UNIT_CONFLICTS
, service
)) < 0)
1837 if ((r
= unit_add_dependency(runlevel
, UNIT_BEFORE
, service
)) < 0)
1854 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
1855 [SERVICE_DEAD
] = "dead",
1856 [SERVICE_START_PRE
] = "start-pre",
1857 [SERVICE_START
] = "start",
1858 [SERVICE_START_POST
] = "start-post",
1859 [SERVICE_RUNNING
] = "running",
1860 [SERVICE_RELOAD
] = "reload",
1861 [SERVICE_STOP
] = "stop",
1862 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
1863 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
1864 [SERVICE_STOP_POST
] = "stop-post",
1865 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
1866 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
1867 [SERVICE_MAINTAINANCE
] = "maintainance",
1868 [SERVICE_AUTO_RESTART
] = "auto-restart",
1871 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
1873 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
1874 [SERVICE_ONCE
] = "once",
1875 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
1876 [SERVICE_RESTART_ALWAYS
] = "restart-on-failure",
1879 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
1881 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
1882 [SERVICE_FORKING
] = "forking",
1883 [SERVICE_SIMPLE
] = "simple",
1884 [SERVICE_FINISH
] = "finish"
1887 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
1889 static const char* const service_exec_command_table
[_SERVICE_EXEC_MAX
] = {
1890 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
1891 [SERVICE_EXEC_START
] = "ExecStart",
1892 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
1893 [SERVICE_EXEC_RELOAD
] = "ExecReload",
1894 [SERVICE_EXEC_STOP
] = "ExecStop",
1895 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
1898 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
1900 const UnitVTable service_vtable
= {
1901 .suffix
= ".service",
1903 .init
= service_init
,
1904 .done
= service_done
,
1906 .dump
= service_dump
,
1908 .start
= service_start
,
1909 .stop
= service_stop
,
1910 .reload
= service_reload
,
1912 .can_reload
= service_can_reload
,
1914 .active_state
= service_active_state
,
1916 .sigchld_event
= service_sigchld_event
,
1917 .timer_event
= service_timer_event
,
1919 .cgroup_notify_empty
= service_cgroup_notify_event
,
1921 .enumerate
= service_enumerate