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
,
46 "/boot.d", SPECIAL_BASIC_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 free(s
->sysv_runlevels
);
78 s
->sysv_runlevels
= NULL
;
80 exec_context_done(&s
->exec_context
);
81 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_MAX
);
82 s
->control_command
= NULL
;
84 /* This will leak a process, but at least no memory or any of
86 if (s
->main_pid
> 0) {
87 unit_unwatch_pid(u
, s
->main_pid
);
91 if (s
->control_pid
> 0) {
92 unit_unwatch_pid(u
, s
->control_pid
);
96 unit_unwatch_timer(u
, &s
->timer_watch
);
99 static int sysv_translate_name(const char *name
, char **_r
) {
101 static const char * const table
[] = {
102 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
103 "$network", SPECIAL_NETWORK_TARGET
,
104 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
105 "$portmap", SPECIAL_RPCBIND_TARGET
,
106 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
107 "$syslog", SPECIAL_SYSLOG_TARGET
,
108 "$time", SPECIAL_RTC_SET_TARGET
114 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
115 if (streq(table
[i
], name
)) {
116 if (!(r
= strdup(table
[i
+1])))
125 if (asprintf(&r
, "%s.service", name
) < 0)
136 static int sysv_chkconfig_order(Service
*s
) {
142 if (s
->sysv_start_priority
< 0)
145 /* For each pair of services where at least one lacks a LSB
146 * header, we use the start priority value to order things. */
148 LIST_FOREACH(units_per_type
, other
, UNIT(s
)->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
152 t
= (Service
*) other
;
157 if (t
->sysv_start_priority
< 0)
160 if (s
->sysv_has_lsb
&& t
->sysv_has_lsb
)
163 if (t
->sysv_start_priority
< s
->sysv_start_priority
)
165 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
170 /* FIXME: Maybe we should compare the name here lexicographically? */
172 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
))) < 0)
179 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
182 if (!(c
= new0(ExecCommand
, 1)))
185 if (!(c
->path
= strdup(path
))) {
190 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
199 static int sysv_exec_commands(Service
*s
) {
203 assert(s
->sysv_path
);
205 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
207 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
209 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
211 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
213 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
215 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
220 static int priority_from_rcd(Service
*s
, const char *init_script
) {
224 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvrcnd_path
)
225 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
230 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0)
238 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
243 while ((de
= readdir(d
))) {
246 if (ignore_file(de
->d_name
))
249 if (de
->d_name
[0] != 'S')
252 if (strlen(de
->d_name
) < 4)
255 if (!streq(de
->d_name
+ 3, init_script
))
258 /* Yay, we found it! Now decode the priority */
260 a
= undecchar(de
->d_name
[1]);
261 b
= undecchar(de
->d_name
[2]);
266 s
->sysv_start_priority
= a
*10 + b
;
268 log_debug("Determined priority %i from link farm for %s", s
->sysv_start_priority
, unit_id(UNIT(s
)));
280 static int service_load_sysv_path(Service
*s
, const char *path
, UnitLoadState
*new_state
) {
298 if (!(f
= fopen(path
, "re"))) {
299 r
= errno
== ENOENT
? 0 : -errno
;
303 s
->type
= SERVICE_FORKING
;
304 s
->restart
= SERVICE_ONCE
;
307 if (!(s
->sysv_path
= strdup(path
))) {
313 char l
[LINE_MAX
], *t
;
315 if (!fgets(l
, sizeof(l
), f
)) {
320 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
330 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
332 s
->sysv_has_lsb
= true;
336 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
342 t
+= strspn(t
, WHITESPACE
);
344 if (state
== NORMAL
) {
346 /* Try to parse Red Hat style chkconfig headers */
348 if (startswith(t
, "chkconfig:")) {
350 char runlevels
[16], *k
;
354 if (sscanf(t
+10, "%15s %i %*i",
356 &start_priority
) != 2) {
358 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
362 if (start_priority
< 0 || start_priority
> 99)
363 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
365 s
->sysv_start_priority
= start_priority
;
367 char_array_0(runlevels
);
368 k
= delete_chars(runlevels
, WHITESPACE
"-");
373 if (!(d
= strdup(k
))) {
378 free(s
->sysv_runlevels
);
379 s
->sysv_runlevels
= d
;
383 } else if (startswith(t
, "description:")) {
385 size_t k
= strlen(t
);
388 if (t
[k
-1] == '\\') {
393 if (!(d
= strdup(strstrip(t
+12)))) {
398 free(u
->meta
.description
);
399 u
->meta
.description
= d
;
401 } else if (startswith(t
, "pidfile:")) {
408 if (!path_is_absolute(fn
)) {
409 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
413 if (!(fn
= strdup(fn
))) {
422 } else if (state
== DESCRIPTION
) {
424 /* Try to parse Red Hat style description
427 size_t k
= strlen(t
);
435 assert(u
->meta
.description
);
436 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
441 free(u
->meta
.description
);
442 u
->meta
.description
= d
;
444 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
446 if (startswith(t
, "Provides:")) {
452 FOREACH_WORD(w
, z
, t
+9, i
) {
455 if (!(n
= strndup(w
, z
))) {
460 r
= sysv_translate_name(n
, &m
);
469 if (unit_name_to_type(m
) == UNIT_SERVICE
)
470 r
= unit_add_name(u
, m
);
472 if ((r
= unit_add_dependency_by_name_inverse(u
, UNIT_REQUIRES
, m
)) >= 0)
473 r
= unit_add_dependency_by_name(u
, UNIT_BEFORE
, m
);
482 } else if (startswith(t
, "Required-Start:") ||
483 startswith(t
, "Should-Start:")) {
489 FOREACH_WORD(w
, z
, strchr(t
, ':')+1, i
) {
492 if (!(n
= strndup(w
, z
))) {
497 r
= sysv_translate_name(n
, &m
);
506 r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, m
);
512 } else if (startswith(t
, "Default-Start:")) {
517 k
= delete_chars(t
+14, WHITESPACE
"-");
520 if (!(d
= strdup(k
))) {
525 free(s
->sysv_runlevels
);
526 s
->sysv_runlevels
= d
;
529 } else if (startswith(t
, "Description:")) {
532 state
= LSB_DESCRIPTION
;
534 if (!(d
= strdup(strstrip(t
+12)))) {
539 free(u
->meta
.description
);
540 u
->meta
.description
= d
;
542 } else if (startswith(t
, "Short-Description:") &&
543 !u
->meta
.description
) {
546 /* We use the short description only
547 * if no long description is set. */
551 if (!(d
= strdup(strstrip(t
+18)))) {
556 u
->meta
.description
= d
;
558 } else if (state
== LSB_DESCRIPTION
) {
560 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
563 assert(u
->meta
.description
);
564 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
569 free(u
->meta
.description
);
570 u
->meta
.description
= d
;
577 /* If init scripts have no LSB header, then we enforce the
578 * ordering via the chkconfig priorities. We try to determine
579 * a priority for *all* init scripts here, since they are
580 * needed as soon as at least one non-LSB script is used. */
582 if (s
->sysv_start_priority
< 0) {
583 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", unit_id(u
));
585 if ((r
= priority_from_rcd(s
, file_name_from_path(path
))) < 0)
588 if (s
->sysv_start_priority
< 0)
589 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", unit_id(u
));
592 if ((r
= sysv_exec_commands(s
)) < 0)
595 if (!s
->sysv_runlevels
|| chars_intersect("12345", s
->sysv_runlevels
)) {
596 /* If there a runlevels configured for this service
597 * but none of the standard ones, then we assume this
598 * is some special kind of service (which might be
599 * needed for early boot) and don't create any links
602 if ((r
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
)) < 0 ||
603 (r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_BASIC_TARGET
)) < 0)
607 *new_state
= UNIT_LOADED
;
618 static int service_load_sysv_name(Service
*s
, const char *name
, UnitLoadState
*new_state
) {
624 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvinit_path
) {
628 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
631 assert(endswith(path
, ".service"));
632 path
[strlen(path
)-8] = 0;
634 r
= service_load_sysv_path(s
, path
, new_state
);
640 if (*new_state
!= UNIT_STUB
)
647 static int service_load_sysv(Service
*s
, UnitLoadState
*new_state
) {
655 /* Load service data from SysV init scripts, preferably with
658 if (strv_isempty(UNIT(s
)->meta
.manager
->sysvinit_path
))
661 if ((t
= unit_id(UNIT(s
))))
662 if ((r
= service_load_sysv_name(s
, t
, new_state
)) < 0)
665 if (*new_state
== UNIT_STUB
)
666 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
667 if ((r
== service_load_sysv_name(s
, t
, new_state
)) < 0)
670 if (*new_state
!= UNIT_STUB
)
677 static int service_init(Unit
*u
, UnitLoadState
*new_state
) {
679 Service
*s
= SERVICE(u
);
683 assert(*new_state
== UNIT_STUB
);
685 /* First, reset everything to the defaults, in case this is a
691 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
692 s
->restart_usec
= DEFAULT_RESTART_USEC
;
694 exec_context_init(&s
->exec_context
);
696 s
->timer_watch
.type
= WATCH_INVALID
;
698 s
->state
= SERVICE_DEAD
;
700 s
->sysv_start_priority
= -1;
701 s
->permissions_start_only
= false;
702 s
->root_directory_start_only
= false;
703 s
->valid_no_process
= false;
705 s
->sysv_has_lsb
= false;
706 s
->main_pid
= s
->control_pid
= 0;
707 s
->main_pid_known
= false;
710 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
712 /* Load a .service file */
713 if ((r
= unit_load_fragment(u
, new_state
)) < 0)
716 /* Load a classic init script as a fallback, if we couldn't find anything */
717 if (*new_state
== UNIT_STUB
)
718 if ((r
= service_load_sysv(s
, new_state
)) < 0)
721 /* Still nothing found? Then let's give up */
722 if (*new_state
== UNIT_STUB
)
725 /* We were able to load something, then let's add in the
726 * dropin directories. */
727 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
730 /* This is a new unit? Then let's add in some extras */
731 if (*new_state
== UNIT_LOADED
) {
732 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
735 if ((r
= unit_add_default_cgroup(u
)) < 0)
738 if ((r
= sysv_chkconfig_order(s
)) < 0)
745 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
747 ServiceExecCommand c
;
748 Service
*s
= SERVICE(u
);
754 p2
= strappend(prefix
, "\t");
755 prefix2
= p2
? p2
: prefix
;
758 "%sService State: %s\n"
759 "%sPermissionsStartOnly: %s\n"
760 "%sRootDirectoryStartOnly: %s\n"
761 "%sValidNoProcess: %s\n"
764 prefix
, service_state_to_string(s
->state
),
765 prefix
, yes_no(s
->permissions_start_only
),
766 prefix
, yes_no(s
->root_directory_start_only
),
767 prefix
, yes_no(s
->valid_no_process
),
768 prefix
, kill_mode_to_string(s
->kill_mode
),
769 prefix
, service_type_to_string(s
->type
));
771 if (s
->control_pid
> 0)
773 "%sControl PID: %llu\n",
774 prefix
, (unsigned long long) s
->control_pid
);
778 "%sMain PID: %llu\n",
779 prefix
, (unsigned long long) s
->main_pid
);
784 prefix
, s
->pid_file
);
786 exec_context_dump(&s
->exec_context
, f
, prefix
);
788 for (c
= 0; c
< _SERVICE_EXEC_MAX
; c
++) {
790 if (!s
->exec_command
[c
])
793 fprintf(f
, "%s→ %s:\n",
794 prefix
, service_exec_command_to_string(c
));
796 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
801 "%sSysV Init Script Path: %s\n"
802 "%sSysV Init Script has LSB Header: %s\n",
803 prefix
, s
->sysv_path
,
804 prefix
, yes_no(s
->sysv_has_lsb
));
806 if (s
->sysv_start_priority
>= 0)
808 "%sSysVStartPriority: %i\n",
809 prefix
, s
->sysv_start_priority
);
811 if (s
->sysv_runlevels
)
812 fprintf(f
, "%sSysVRunLevels: %s\n",
813 prefix
, s
->sysv_runlevels
);
818 static int service_load_pid_file(Service
*s
) {
825 if (s
->main_pid_known
)
831 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
834 if ((r
= safe_atolu(k
, &p
)) < 0) {
839 if ((unsigned long) (pid_t
) p
!= p
)
843 s
->main_pid_known
= true;
848 static int service_get_sockets(Service
*s
, Set
**_set
) {
857 /* Collects all Socket objects that belong to this
858 * service. Note that a service might have multiple sockets
859 * via multiple names. */
861 if (!(set
= set_new(NULL
, NULL
)))
864 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
868 /* Look for all socket objects that go by any of our
869 * units and collect their fds */
871 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
876 p
= manager_get_unit(UNIT(s
)->meta
.manager
, k
);
881 if ((r
= set_put(set
, p
)) < 0)
894 static int service_notify_sockets(Service
*s
) {
902 /* Notifies all our sockets when we die */
904 if ((r
= service_get_sockets(s
, &set
)) < 0)
907 SET_FOREACH(sock
, set
, i
)
908 socket_notify_service_dead(sock
);
915 static void service_set_state(Service
*s
, ServiceState state
) {
916 ServiceState old_state
;
919 old_state
= s
->state
;
922 if (state
!= SERVICE_START_PRE
&&
923 state
!= SERVICE_START
&&
924 state
!= SERVICE_START_POST
&&
925 state
!= SERVICE_RELOAD
&&
926 state
!= SERVICE_STOP
&&
927 state
!= SERVICE_STOP_SIGTERM
&&
928 state
!= SERVICE_STOP_SIGKILL
&&
929 state
!= SERVICE_STOP_POST
&&
930 state
!= SERVICE_FINAL_SIGTERM
&&
931 state
!= SERVICE_FINAL_SIGKILL
&&
932 state
!= SERVICE_AUTO_RESTART
)
933 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
935 if (state
!= SERVICE_START
&&
936 state
!= SERVICE_START_POST
&&
937 state
!= SERVICE_RUNNING
&&
938 state
!= SERVICE_RELOAD
&&
939 state
!= SERVICE_STOP
&&
940 state
!= SERVICE_STOP_SIGTERM
&&
941 state
!= SERVICE_STOP_SIGKILL
)
942 if (s
->main_pid
> 0) {
943 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
947 if (state
!= SERVICE_START_PRE
&&
948 state
!= SERVICE_START
&&
949 state
!= SERVICE_START_POST
&&
950 state
!= SERVICE_RELOAD
&&
951 state
!= SERVICE_STOP
&&
952 state
!= SERVICE_STOP_SIGTERM
&&
953 state
!= SERVICE_STOP_SIGKILL
&&
954 state
!= SERVICE_STOP_POST
&&
955 state
!= SERVICE_FINAL_SIGTERM
&&
956 state
!= SERVICE_FINAL_SIGKILL
)
957 if (s
->control_pid
> 0) {
958 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
962 if (state
!= SERVICE_START_PRE
&&
963 state
!= SERVICE_START
&&
964 state
!= SERVICE_START_POST
&&
965 state
!= SERVICE_RELOAD
&&
966 state
!= SERVICE_STOP
&&
967 state
!= SERVICE_STOP_POST
)
968 s
->control_command
= NULL
;
970 if (state
== SERVICE_DEAD
||
971 state
== SERVICE_STOP
||
972 state
== SERVICE_STOP_SIGTERM
||
973 state
== SERVICE_STOP_SIGKILL
||
974 state
== SERVICE_STOP_POST
||
975 state
== SERVICE_FINAL_SIGTERM
||
976 state
== SERVICE_FINAL_SIGKILL
||
977 state
== SERVICE_MAINTAINANCE
||
978 state
== SERVICE_AUTO_RESTART
)
979 service_notify_sockets(s
);
981 if (old_state
== state
)
984 log_debug("%s changed %s → %s", unit_id(UNIT(s
)), service_state_to_string(old_state
), service_state_to_string(state
));
986 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
989 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
1001 if ((r
= service_get_sockets(s
, &set
)) < 0)
1004 SET_FOREACH(sock
, set
, i
) {
1008 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
1020 if (!(t
= new(int, rn_fds
+cn_fds
))) {
1026 memcpy(t
, rfds
, rn_fds
);
1027 memcpy(t
+rn_fds
, cfds
, cn_fds
);
1032 rn_fds
= rn_fds
+cn_fds
;
1050 static int service_spawn(
1055 bool apply_permissions
,
1069 if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
1073 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1076 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1078 if ((r
= exec_spawn(c
,
1083 UNIT(s
)->meta
.cgroup_bondings
,
1087 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1088 /* FIXME: we need to do something here */
1100 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1105 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
1112 if (allow_restart
&&
1113 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1114 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
1116 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
1119 service_set_state(s
, SERVICE_AUTO_RESTART
);
1121 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
1126 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s
)), strerror(-r
));
1127 service_enter_dead(s
, false, false);
1130 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
1132 static void service_enter_stop_post(Service
*s
, bool success
) {
1139 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
]))
1140 if ((r
= service_spawn(s
,
1144 !s
->permissions_start_only
,
1145 !s
->root_directory_start_only
,
1146 &s
->control_pid
)) < 0)
1150 service_set_state(s
, SERVICE_STOP_POST
);
1152 if (!s
->control_command
)
1153 service_enter_dead(s
, true, true);
1158 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1159 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1162 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
1171 if (s
->main_pid
> 0 || s
->control_pid
> 0) {
1174 sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
1176 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
1178 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
1179 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
1187 if (s
->main_pid
> 0) {
1188 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->main_pid
: -s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
1194 if (s
->control_pid
> 0) {
1195 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1206 service_set_state(s
, state
);
1208 if (s
->main_pid
<= 0 && s
->control_pid
<= 0)
1209 service_enter_dead(s
, true, true);
1214 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s
)), strerror(-r
));
1218 service_set_state(s
, state
);
1219 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1220 service_enter_stop_post(s
, false);
1222 service_enter_dead(s
, false, true);
1225 static void service_enter_stop(Service
*s
, bool success
) {
1232 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
]))
1233 if ((r
= service_spawn(s
,
1237 !s
->permissions_start_only
,
1238 !s
->root_directory_start_only
,
1239 &s
->control_pid
)) < 0)
1242 service_set_state(s
, SERVICE_STOP
);
1244 if (!s
->control_command
)
1245 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1250 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1251 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1254 static void service_enter_start_post(Service
*s
) {
1258 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
]))
1259 if ((r
= service_spawn(s
,
1263 !s
->permissions_start_only
,
1264 !s
->root_directory_start_only
,
1265 &s
->control_pid
)) < 0)
1269 service_set_state(s
, SERVICE_START_POST
);
1271 if (!s
->control_command
)
1272 service_set_state(s
, SERVICE_RUNNING
);
1277 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1278 service_enter_stop(s
, false);
1281 static void service_enter_start(Service
*s
) {
1287 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1288 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1290 if ((r
= service_spawn(s
,
1291 s
->exec_command
[SERVICE_EXEC_START
],
1292 s
->type
== SERVICE_FORKING
,
1299 service_set_state(s
, SERVICE_START
);
1301 if (s
->type
== SERVICE_SIMPLE
) {
1302 /* For simple services we immediately start
1303 * the START_POST binaries. */
1306 s
->main_pid_known
= true;
1307 service_enter_start_post(s
);
1309 } else if (s
->type
== SERVICE_FORKING
) {
1311 /* For forking services we wait until the start
1312 * process exited. */
1314 s
->control_pid
= pid
;
1315 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1316 } else if (s
->type
== SERVICE_FINISH
) {
1318 /* For finishing services we wait until the start
1319 * process exited, too, but it is our main process. */
1322 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1324 assert_not_reached("Unknown service type");
1329 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s
)), strerror(-r
));
1330 service_enter_stop(s
, false);
1333 static void service_enter_start_pre(Service
*s
) {
1338 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
]))
1339 if ((r
= service_spawn(s
,
1343 !s
->permissions_start_only
,
1344 !s
->root_directory_start_only
,
1345 &s
->control_pid
)) < 0)
1348 service_set_state(s
, SERVICE_START_PRE
);
1350 if (!s
->control_command
)
1351 service_enter_start(s
);
1356 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1357 service_enter_dead(s
, false, true);
1360 static void service_enter_restart(Service
*s
) {
1364 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1367 log_debug("%s scheduled restart job.", unit_id(UNIT(s
)));
1368 service_enter_dead(s
, true, false);
1373 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s
)), strerror(-r
));
1374 service_enter_dead(s
, false, false);
1377 static void service_enter_reload(Service
*s
) {
1382 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
]))
1383 if ((r
= service_spawn(s
,
1387 !s
->permissions_start_only
,
1388 !s
->root_directory_start_only
,
1389 &s
->control_pid
)) < 0)
1392 service_set_state(s
, SERVICE_RELOAD
);
1394 if (!s
->control_command
)
1395 service_set_state(s
, SERVICE_RUNNING
);
1400 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1401 service_enter_stop(s
, false);
1404 static void service_run_next(Service
*s
, bool success
) {
1408 assert(s
->control_command
);
1409 assert(s
->control_command
->command_next
);
1414 s
->control_command
= s
->control_command
->command_next
;
1416 if ((r
= service_spawn(s
,
1420 !s
->permissions_start_only
,
1421 !s
->root_directory_start_only
,
1422 &s
->control_pid
)) < 0)
1428 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1430 if (s
->state
== SERVICE_STOP
)
1431 service_enter_stop_post(s
, false);
1432 else if (s
->state
== SERVICE_STOP_POST
)
1433 service_enter_dead(s
, false, true);
1435 service_enter_stop(s
, false);
1438 static int service_start(Unit
*u
) {
1439 Service
*s
= SERVICE(u
);
1443 /* We cannot fulfill this request right now, try again later
1445 if (s
->state
== SERVICE_STOP
||
1446 s
->state
== SERVICE_STOP_SIGTERM
||
1447 s
->state
== SERVICE_STOP_SIGKILL
||
1448 s
->state
== SERVICE_STOP_POST
||
1449 s
->state
== SERVICE_FINAL_SIGTERM
||
1450 s
->state
== SERVICE_FINAL_SIGKILL
)
1453 /* Already on it! */
1454 if (s
->state
== SERVICE_START_PRE
||
1455 s
->state
== SERVICE_START
||
1456 s
->state
== SERVICE_START_POST
)
1459 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1461 /* Make sure we don't enter a busy loop of some kind. */
1462 if (!ratelimit_test(&s
->ratelimit
)) {
1463 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u
));
1468 s
->main_pid_known
= false;
1470 service_enter_start_pre(s
);
1474 static int service_stop(Unit
*u
) {
1475 Service
*s
= SERVICE(u
);
1479 if (s
->state
== SERVICE_START_PRE
||
1480 s
->state
== SERVICE_START
||
1481 s
->state
== SERVICE_START_POST
||
1482 s
->state
== SERVICE_RELOAD
)
1485 if (s
->state
== SERVICE_AUTO_RESTART
) {
1486 service_set_state(s
, SERVICE_DEAD
);
1490 assert(s
->state
== SERVICE_RUNNING
);
1492 service_enter_stop(s
, true);
1496 static int service_reload(Unit
*u
) {
1497 Service
*s
= SERVICE(u
);
1501 assert(s
->state
== SERVICE_RUNNING
);
1503 service_enter_reload(s
);
1507 static bool service_can_reload(Unit
*u
) {
1508 Service
*s
= SERVICE(u
);
1512 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1515 static UnitActiveState
service_active_state(Unit
*u
) {
1518 return state_translation_table
[SERVICE(u
)->state
];
1521 static int main_pid_good(Service
*s
) {
1524 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1527 /* If we know the pid file, then lets just check if it is
1529 if (s
->main_pid_known
)
1530 return s
->main_pid
> 0;
1532 /* We don't know the pid */
1536 static bool control_pid_good(Service
*s
) {
1539 return s
->control_pid
> 0;
1542 static int cgroup_good(Service
*s
) {
1545 if (s
->valid_no_process
)
1548 return cgroup_bonding_is_empty_list(UNIT(s
)->meta
.cgroup_bondings
);
1551 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1552 Service
*s
= SERVICE(u
);
1558 success
= code
== CLD_EXITED
&& status
== 0;
1559 s
->failure
= s
->failure
|| !success
;
1561 if (s
->main_pid
== pid
) {
1563 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1566 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1567 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1568 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1571 log_debug("%s: main process exited, code=%s, status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
1573 /* The service exited, so the service is officially
1578 case SERVICE_START_POST
:
1579 case SERVICE_RELOAD
:
1581 /* Need to wait until the operation is
1586 assert(s
->type
== SERVICE_FINISH
);
1588 /* This was our main goal, so let's go on */
1590 service_enter_start_post(s
);
1592 service_enter_stop(s
, false);
1595 case SERVICE_RUNNING
:
1596 service_enter_stop(s
, success
);
1599 case SERVICE_STOP_SIGTERM
:
1600 case SERVICE_STOP_SIGKILL
:
1602 if (!control_pid_good(s
))
1603 service_enter_stop_post(s
, success
);
1605 /* If there is still a control process, wait for that first */
1609 assert_not_reached("Uh, main process died at wrong time.");
1612 } else if (s
->control_pid
== pid
) {
1613 assert(s
->control_command
);
1615 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1618 log_debug("%s: control process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
1620 /* If we are shutting things down anyway we
1621 * don't care about failing commands. */
1623 if (s
->control_command
->command_next
&&
1624 (success
|| (s
->state
== SERVICE_STOP
|| s
->state
== SERVICE_STOP_POST
)))
1626 /* There is another command to *
1627 * execute, so let's do that. */
1629 service_run_next(s
, success
);
1632 /* No further commands for this step, so let's
1633 * figure out what to do next */
1635 log_debug("%s got final SIGCHLD for state %s", unit_id(u
), service_state_to_string(s
->state
));
1639 case SERVICE_START_PRE
:
1641 service_enter_start(s
);
1643 service_enter_stop(s
, false);
1647 assert(s
->type
== SERVICE_FORKING
);
1649 /* Let's try to load the pid
1650 * file here if we can. We
1651 * ignore the return value,
1652 * since the PID file might
1653 * actually be created by a
1654 * START_POST script */
1658 service_load_pid_file(s
);
1660 service_enter_start_post(s
);
1662 service_enter_stop(s
, false);
1666 case SERVICE_START_POST
:
1667 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
1670 /* Hmm, let's see if we can
1671 * load the pid now after the
1672 * start-post scripts got
1675 if ((r
= service_load_pid_file(s
)) < 0)
1676 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s
)), s
->pid_file
, strerror(-r
));
1681 case SERVICE_RELOAD
:
1683 if (main_pid_good(s
) != 0 && cgroup_good(s
) != 0)
1684 service_set_state(s
, SERVICE_RUNNING
);
1686 service_enter_stop(s
, true);
1688 service_enter_stop(s
, false);
1693 if (main_pid_good(s
) > 0)
1694 /* Still not dead and we know the PID? Let's go hunting. */
1695 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
1697 service_enter_stop_post(s
, success
);
1700 case SERVICE_STOP_SIGTERM
:
1701 case SERVICE_STOP_SIGKILL
:
1702 if (main_pid_good(s
) <= 0)
1703 service_enter_stop_post(s
, success
);
1705 /* If there is still a service
1706 * process around, wait until
1707 * that one quit, too */
1710 case SERVICE_STOP_POST
:
1711 case SERVICE_FINAL_SIGTERM
:
1712 case SERVICE_FINAL_SIGKILL
:
1713 service_enter_dead(s
, success
, true);
1717 assert_not_reached("Uh, control process died at wrong time.");
1721 assert_not_reached("Got SIGCHLD for unkown PID");
1724 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
1725 Service
*s
= SERVICE(u
);
1728 assert(elapsed
== 1);
1730 assert(w
== &s
->timer_watch
);
1734 case SERVICE_START_PRE
:
1736 case SERVICE_START_POST
:
1737 case SERVICE_RELOAD
:
1738 log_warning("%s operation timed out. Stopping.", unit_id(u
));
1739 service_enter_stop(s
, false);
1743 log_warning("%s stopping timed out. Terminating.", unit_id(u
));
1744 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1747 case SERVICE_STOP_SIGTERM
:
1748 log_warning("%s stopping timed out. Killing.", unit_id(u
));
1749 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
1752 case SERVICE_STOP_SIGKILL
:
1753 /* Uh, wie sent a SIGKILL and it is still not gone?
1754 * Must be something we cannot kill, so let's just be
1755 * weirded out and continue */
1757 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u
));
1758 service_enter_stop_post(s
, false);
1761 case SERVICE_STOP_POST
:
1762 log_warning("%s stopping timed out (2). Terminating.", unit_id(u
));
1763 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1766 case SERVICE_FINAL_SIGTERM
:
1767 log_warning("%s stopping timed out (2). Killing.", unit_id(u
));
1768 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
1771 case SERVICE_FINAL_SIGKILL
:
1772 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u
));
1773 service_enter_dead(s
, false, true);
1776 case SERVICE_AUTO_RESTART
:
1777 log_debug("%s holdoff time over, scheduling restart.", unit_id(u
));
1778 service_enter_restart(s
);
1782 assert_not_reached("Timeout at wrong time.");
1786 static void service_cgroup_notify_event(Unit
*u
) {
1787 Service
*s
= SERVICE(u
);
1791 log_debug("%s: cgroup is empty", unit_id(u
));
1795 /* Waiting for SIGCHLD is usually more interesting,
1796 * because it includes return codes/signals. Which is
1797 * why we ignore the cgroup events for most cases,
1798 * except when we don't know pid which to expect the
1801 case SERVICE_RUNNING
:
1803 if (!s
->valid_no_process
&& main_pid_good(s
) <= 0)
1804 service_enter_stop(s
, true);
1813 static int service_enumerate(Manager
*m
) {
1817 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
1822 STRV_FOREACH(p
, m
->sysvrcnd_path
)
1823 for (i
= 0; i
< ELEMENTSOF(rcnd_table
); i
+= 2) {
1828 if (asprintf(&path
, "%s/%s", *p
, rcnd_table
[i
]) < 0) {
1836 if (!(d
= opendir(path
))) {
1837 if (errno
!= ENOENT
)
1838 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
1843 while ((de
= readdir(d
))) {
1844 Unit
*runlevel
, *service
;
1846 if (ignore_file(de
->d_name
))
1849 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
1852 if (strlen(de
->d_name
) < 4)
1857 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd_table
[i
], de
->d_name
) < 0) {
1862 if (access(fpath
, X_OK
) < 0) {
1864 if (errno
!= ENOENT
)
1865 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
1872 if (asprintf(&name
, "%s.service", de
->d_name
+3) < 0) {
1877 if ((r
= manager_load_unit(m
, name
, &service
)) < 0)
1880 if ((r
= manager_load_unit(m
, rcnd_table
[i
+1], &runlevel
)) < 0)
1883 if (de
->d_name
[0] == 'S') {
1884 if ((r
= unit_add_dependency(runlevel
, UNIT_WANTS
, service
)) < 0)
1887 if ((r
= unit_add_dependency(runlevel
, UNIT_AFTER
, service
)) < 0)
1890 } else if (de
->d_name
[0] == 'K' &&
1891 (streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL0_TARGET
) ||
1892 streq(rcnd_table
[i
+1], SPECIAL_RUNLEVEL6_TARGET
))) {
1894 /* We honour K links only for
1895 * halt/reboot. For the normal
1896 * runlevels we assume the
1898 * implicitly added by the
1901 if ((r
= unit_add_dependency(runlevel
, UNIT_CONFLICTS
, service
)) < 0)
1904 if ((r
= unit_add_dependency(runlevel
, UNIT_BEFORE
, service
)) < 0)
1921 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
1922 [SERVICE_DEAD
] = "dead",
1923 [SERVICE_START_PRE
] = "start-pre",
1924 [SERVICE_START
] = "start",
1925 [SERVICE_START_POST
] = "start-post",
1926 [SERVICE_RUNNING
] = "running",
1927 [SERVICE_RELOAD
] = "reload",
1928 [SERVICE_STOP
] = "stop",
1929 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
1930 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
1931 [SERVICE_STOP_POST
] = "stop-post",
1932 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
1933 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
1934 [SERVICE_MAINTAINANCE
] = "maintainance",
1935 [SERVICE_AUTO_RESTART
] = "auto-restart",
1938 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
1940 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
1941 [SERVICE_ONCE
] = "once",
1942 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
1943 [SERVICE_RESTART_ALWAYS
] = "restart-on-failure",
1946 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
1948 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
1949 [SERVICE_FORKING
] = "forking",
1950 [SERVICE_SIMPLE
] = "simple",
1951 [SERVICE_FINISH
] = "finish"
1954 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
1956 static const char* const service_exec_command_table
[_SERVICE_EXEC_MAX
] = {
1957 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
1958 [SERVICE_EXEC_START
] = "ExecStart",
1959 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
1960 [SERVICE_EXEC_RELOAD
] = "ExecReload",
1961 [SERVICE_EXEC_STOP
] = "ExecStop",
1962 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
1965 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
1967 const UnitVTable service_vtable
= {
1968 .suffix
= ".service",
1970 .init
= service_init
,
1971 .done
= service_done
,
1973 .dump
= service_dump
,
1975 .start
= service_start
,
1976 .stop
= service_stop
,
1977 .reload
= service_reload
,
1979 .can_reload
= service_can_reload
,
1981 .active_state
= service_active_state
,
1983 .sigchld_event
= service_sigchld_event
,
1984 .timer_event
= service_timer_event
,
1986 .cgroup_notify_empty
= service_cgroup_notify_event
,
1988 .enumerate
= service_enumerate