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 UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
39 [SERVICE_DEAD
] = UNIT_INACTIVE
,
40 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
41 [SERVICE_START
] = UNIT_ACTIVATING
,
42 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
43 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
44 [SERVICE_RELOAD
] = UNIT_ACTIVE_RELOADING
,
45 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
46 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
47 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
48 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
49 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
50 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
51 [SERVICE_MAINTAINANCE
] = UNIT_INACTIVE
,
52 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
,
55 static void service_done(Unit
*u
) {
56 Service
*s
= SERVICE(u
);
66 exec_context_done(&s
->exec_context
);
67 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_MAX
);
68 s
->control_command
= NULL
;
70 /* This will leak a process, but at least no memory or any of
72 if (s
->main_pid
> 0) {
73 unit_unwatch_pid(u
, s
->main_pid
);
77 if (s
->control_pid
> 0) {
78 unit_unwatch_pid(u
, s
->control_pid
);
82 unit_unwatch_timer(u
, &s
->timer_watch
);
85 static int sysv_translate_name(const char *name
, char **_r
) {
87 static const char * const table
[] = {
88 "$local_fs", SPECIAL_LOCAL_FS_TARGET
,
89 "$network", SPECIAL_NETWORK_TARGET
,
90 "$named", SPECIAL_NSS_LOOKUP_TARGET
,
91 "$portmap", SPECIAL_RPCBIND_TARGET
,
92 "$remote_fs", SPECIAL_REMOTE_FS_TARGET
,
93 "$syslog", SPECIAL_SYSLOG_TARGET
,
94 "$time", SPECIAL_RTC_SET_TARGET
100 for (i
= 0; i
< ELEMENTSOF(table
); i
+= 2)
101 if (streq(table
[i
], name
)) {
102 if (!(r
= strdup(table
[i
+1])))
111 if (asprintf(&r
, "%s.service", name
) < 0)
122 static int sysv_chkconfig_order(Service
*s
) {
128 if (s
->sysv_start_priority
< 0)
131 /* If no LSB header is found we try to order init scripts via
132 * the start priority of the chkconfig header. */
134 LIST_FOREACH(units_per_type
, other
, UNIT(s
)->meta
.manager
->units_per_type
[UNIT_SERVICE
]) {
138 t
= (Service
*) other
;
143 if (t
->sysv_start_priority
< 0)
146 if (t
->sysv_start_priority
< s
->sysv_start_priority
)
148 else if (t
->sysv_start_priority
> s
->sysv_start_priority
)
153 /* FIXME: Maybe we should compare the name here lexicographically? */
155 if (!(r
= unit_add_dependency(UNIT(s
), d
, UNIT(t
))) < 0)
162 static ExecCommand
*exec_command_new(const char *path
, const char *arg1
) {
165 if (!(c
= new0(ExecCommand
, 1)))
168 if (!(c
->path
= strdup(path
))) {
173 if (!(c
->argv
= strv_new(path
, arg1
, NULL
))) {
182 static int sysv_exec_commands(Service
*s
) {
186 assert(s
->sysv_path
);
188 if (!(c
= exec_command_new(s
->sysv_path
, "start")))
190 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_START
, c
);
192 if (!(c
= exec_command_new(s
->sysv_path
, "stop")))
194 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_STOP
, c
);
196 if (!(c
= exec_command_new(s
->sysv_path
, "reload")))
198 exec_command_append_list(s
->exec_command
+SERVICE_EXEC_RELOAD
, c
);
203 static int service_load_sysv_path(Service
*s
, const char *path
) {
214 bool has_lsb
= false;
218 if (!(f
= fopen(path
, "re"))) {
219 r
= errno
== ENOENT
? 0 : -errno
;
223 s
->type
= SERVICE_FORKING
;
224 s
->restart
= SERVICE_ONCE
;
227 if (!(s
->sysv_path
= strdup(path
))) {
233 char l
[LINE_MAX
], *t
;
235 if (!fgets(l
, sizeof(l
), f
)) {
240 log_error("Failed to read configuration file '%s': %s", path
, strerror(-r
));
250 if (state
== NORMAL
&& streq(t
, "### BEGIN INIT INFO")) {
256 if ((state
== LSB_DESCRIPTION
|| state
== LSB
) && streq(t
, "### END INIT INFO")) {
262 t
+= strspn(t
, WHITESPACE
);
264 if (state
== NORMAL
) {
266 /* Try to parse Red Hat style chkconfig headers */
268 if (startswith(t
, "chkconfig:")) {
273 if (sscanf(t
+10, "%*15s %i %*i",
274 &start_priority
) != 1) {
276 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path
, line
);
280 if (start_priority
< 0 || start_priority
> 99) {
281 log_warning("[%s:%u] Start priority out of range. Ignoring.", path
, line
);
285 s
->sysv_start_priority
= start_priority
;
287 } else if (startswith(t
, "description:")) {
289 size_t k
= strlen(t
);
292 if (t
[k
-1] == '\\') {
297 if (!(d
= strdup(strstrip(t
+12)))) {
302 free(u
->meta
.description
);
303 u
->meta
.description
= d
;
305 } else if (startswith(t
, "pidfile:")) {
312 if (!path_is_absolute(fn
)) {
313 log_warning("[%s:%u] PID file not absolute. Ignoring.", path
, line
);
317 if (!(fn
= strdup(fn
))) {
326 } else if (state
== DESCRIPTION
) {
328 /* Try to parse Red Hat style description
331 size_t k
= strlen(t
);
339 assert(u
->meta
.description
);
340 if (asprintf(&d
, "%s %s", u
->meta
.description
, strstrip(t
)) < 0) {
345 free(u
->meta
.description
);
346 u
->meta
.description
= d
;
348 } else if (state
== LSB
|| state
== LSB_DESCRIPTION
) {
350 if (startswith(t
, "Provides:")) {
356 FOREACH_WORD(w
, z
, t
+9, i
) {
359 if (!(n
= strndup(w
, z
))) {
364 r
= sysv_translate_name(n
, &m
);
373 r
= unit_add_name(u
, m
);
380 } else if (startswith(t
, "Required-Start:") ||
381 startswith(t
, "Should-Start:")) {
387 FOREACH_WORD(w
, z
, strchr(t
, ':')+1, i
) {
390 if (!(n
= strndup(w
, z
))) {
395 r
= sysv_translate_name(n
, &m
);
404 if (!(r
= unit_add_dependency_by_name(u
, UNIT_AFTER
, m
)) < 0) {
409 r
= unit_add_dependency_by_name(
411 startswith(t
, "Required-Start:") ? UNIT_REQUIRES
: UNIT_WANTS
,
419 } else if (startswith(t
, "Description:")) {
422 state
= LSB_DESCRIPTION
;
424 if (!(d
= strdup(strstrip(t
+12)))) {
429 free(u
->meta
.description
);
430 u
->meta
.description
= d
;
432 } else if (startswith(t
, "Short-Description:") && !u
->meta
.description
) {
435 /* We use the short description only
436 * if no long description is set. */
440 if (!(d
= strdup(strstrip(t
+18)))) {
445 free(u
->meta
.description
);
446 u
->meta
.description
= d
;
448 } else if (state
== LSB_DESCRIPTION
) {
450 if (startswith(l
, "#\t") || startswith(l
, "# ")) {
453 assert(u
->meta
.description
);
454 if (asprintf(&d
, "%s %s", u
->meta
.description
, t
) < 0) {
459 free(u
->meta
.description
);
460 u
->meta
.description
= d
;
467 /* If the init script has no LSB header, then let's
468 * enforce the ordering via the chkconfig
472 if ((r
= sysv_chkconfig_order(s
)) < 0)
475 if ((r
= sysv_exec_commands(s
)) < 0)
488 static int service_load_sysv_name(Service
*s
, const char *name
) {
494 STRV_FOREACH(p
, UNIT(s
)->meta
.manager
->sysvinit_path
) {
498 if (asprintf(&path
, "%s/%s", *p
, name
) < 0)
501 assert(endswith(path
, ".service"));
502 path
[strlen(path
)-8] = 0;
504 r
= service_load_sysv_path(s
, path
);
514 static int service_load_sysv(Service
*s
) {
521 /* Load service data from SysV init scripts, preferably with
524 if (strv_isempty(UNIT(s
)->meta
.manager
->sysvinit_path
))
527 if ((t
= unit_id(UNIT(s
))))
528 if ((r
= service_load_sysv_name(s
, t
)) != 0)
531 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
)
532 if ((r
== service_load_sysv_name(s
, t
)) != 0)
538 static int service_init(Unit
*u
) {
540 Service
*s
= SERVICE(u
);
544 /* First, reset everything to the defaults, in case this is a
550 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
551 s
->restart_usec
= DEFAULT_RESTART_USEC
;
553 exec_context_init(&s
->exec_context
);
555 s
->timer_watch
.type
= WATCH_INVALID
;
557 s
->state
= SERVICE_DEAD
;
559 s
->sysv_start_priority
= -1;
561 RATELIMIT_INIT(s
->ratelimit
, 10*USEC_PER_SEC
, 5);
563 /* Load a .service file */
564 if ((r
= unit_load_fragment(u
)) < 0) {
569 /* Load a classic init script as a fallback, if we couldn*t find anything */
571 if ((r
= service_load_sysv(s
)) <= 0) {
573 return r
< 0 ? r
: -ENOENT
;
576 /* Load dropin directory data */
577 if ((r
= unit_load_dropin(u
)) < 0) {
585 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
587 ServiceExecCommand c
;
588 Service
*s
= SERVICE(u
);
594 p2
= strappend(prefix
, "\t");
595 prefix2
= p2
? p2
: prefix
;
598 "%sService State: %s\n",
599 prefix
, service_state_to_string(s
->state
));
604 prefix
, s
->pid_file
);
607 exec_context_dump(&s
->exec_context
, f
, prefix
);
609 for (c
= 0; c
< _SERVICE_EXEC_MAX
; c
++) {
611 if (!s
->exec_command
[c
])
614 fprintf(f
, "%s→ %s:\n",
615 prefix
, service_exec_command_to_string(c
));
617 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
622 "%sSysV Init Script Path: %s\n",
623 prefix
, s
->sysv_path
);
625 if (s
->sysv_start_priority
>= 0)
627 "%sSysV Start Priority: %i\n",
628 prefix
, s
->sysv_start_priority
);
633 static int service_load_pid_file(Service
*s
) {
640 if (s
->main_pid_known
)
646 if ((r
= read_one_line_file(s
->pid_file
, &k
)) < 0)
649 if ((r
= safe_atolu(k
, &p
)) < 0) {
654 if ((unsigned long) (pid_t
) p
!= p
)
658 s
->main_pid_known
= true;
663 static int service_get_sockets(Service
*s
, Set
**_set
) {
672 /* Collects all Socket objects that belong to this
673 * service. Note that a service might have multiple sockets
674 * via multiple names. */
676 if (!(set
= set_new(NULL
, NULL
)))
679 SET_FOREACH(t
, UNIT(s
)->meta
.names
, i
) {
683 /* Look for all socket objects that go by any of our
684 * units and collect their fds */
686 if (!(k
= unit_name_change_suffix(t
, ".socket"))) {
691 p
= manager_get_unit(UNIT(s
)->meta
.manager
, k
);
696 if ((r
= set_put(set
, p
)) < 0)
709 static int service_notify_sockets(Service
*s
) {
717 /* Notifies all our sockets when we die */
719 if ((r
= service_get_sockets(s
, &set
)) < 0)
722 SET_FOREACH(sock
, set
, i
)
723 socket_notify_service_dead(sock
);
730 static void service_set_state(Service
*s
, ServiceState state
) {
731 ServiceState old_state
;
734 old_state
= s
->state
;
737 if (state
!= SERVICE_START_PRE
&&
738 state
!= SERVICE_START
&&
739 state
!= SERVICE_START_POST
&&
740 state
!= SERVICE_RELOAD
&&
741 state
!= SERVICE_STOP
&&
742 state
!= SERVICE_STOP_SIGTERM
&&
743 state
!= SERVICE_STOP_SIGKILL
&&
744 state
!= SERVICE_STOP_POST
&&
745 state
!= SERVICE_FINAL_SIGTERM
&&
746 state
!= SERVICE_FINAL_SIGKILL
&&
747 state
!= SERVICE_AUTO_RESTART
)
748 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
750 if (state
!= SERVICE_START
&&
751 state
!= SERVICE_START_POST
&&
752 state
!= SERVICE_RUNNING
&&
753 state
!= SERVICE_RELOAD
&&
754 state
!= SERVICE_STOP
&&
755 state
!= SERVICE_STOP_SIGTERM
&&
756 state
!= SERVICE_STOP_SIGKILL
)
757 if (s
->main_pid
> 0) {
758 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
762 if (state
!= SERVICE_START_PRE
&&
763 state
!= SERVICE_START
&&
764 state
!= SERVICE_START_POST
&&
765 state
!= SERVICE_RELOAD
&&
766 state
!= SERVICE_STOP
&&
767 state
!= SERVICE_STOP_SIGTERM
&&
768 state
!= SERVICE_STOP_SIGKILL
&&
769 state
!= SERVICE_STOP_POST
&&
770 state
!= SERVICE_FINAL_SIGTERM
&&
771 state
!= SERVICE_FINAL_SIGKILL
)
772 if (s
->control_pid
> 0) {
773 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
777 if (state
!= SERVICE_START_PRE
&&
778 state
!= SERVICE_START
&&
779 state
!= SERVICE_START_POST
&&
780 state
!= SERVICE_RELOAD
&&
781 state
!= SERVICE_STOP
&&
782 state
!= SERVICE_STOP_POST
)
783 s
->control_command
= NULL
;
785 if (state
== SERVICE_DEAD
||
786 state
== SERVICE_STOP
||
787 state
== SERVICE_STOP_SIGTERM
||
788 state
== SERVICE_STOP_SIGKILL
||
789 state
== SERVICE_STOP_POST
||
790 state
== SERVICE_FINAL_SIGTERM
||
791 state
== SERVICE_FINAL_SIGKILL
||
792 state
== SERVICE_MAINTAINANCE
||
793 state
== SERVICE_AUTO_RESTART
)
794 service_notify_sockets(s
);
796 log_debug("%s changed %s → %s", unit_id(UNIT(s
)), service_state_to_string(old_state
), service_state_to_string(state
));
798 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
801 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
813 if ((r
= service_get_sockets(s
, &set
)) < 0)
816 SET_FOREACH(sock
, set
, i
) {
820 if ((r
= socket_collect_fds(sock
, &cfds
, &cn_fds
)) < 0)
832 if (!(t
= new(int, rn_fds
+cn_fds
))) {
838 memcpy(t
, rfds
, rn_fds
);
839 memcpy(t
+rn_fds
, cfds
, cn_fds
);
844 rn_fds
= rn_fds
+cn_fds
;
862 static int service_spawn(Service
*s
, ExecCommand
*c
, bool timeout
, bool pass_fds
, pid_t
*_pid
) {
873 if ((r
= service_collect_fds(s
, &fds
, &n_fds
)) < 0)
877 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
880 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
882 if ((r
= exec_spawn(c
, &s
->exec_context
, fds
, n_fds
, &pid
)) < 0)
885 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
886 /* FIXME: we need to do something here */
898 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
903 static void service_enter_dead(Service
*s
, bool success
, bool allow_restart
) {
911 (s
->restart
== SERVICE_RESTART_ALWAYS
||
912 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& !s
->failure
))) {
914 if ((r
= unit_watch_timer(UNIT(s
), s
->restart_usec
, &s
->timer_watch
)) < 0)
917 service_set_state(s
, SERVICE_AUTO_RESTART
);
919 service_set_state(s
, s
->failure
? SERVICE_MAINTAINANCE
: SERVICE_DEAD
);
924 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s
)), strerror(-r
));
925 service_enter_dead(s
, false, false);
928 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
);
930 static void service_enter_stop_post(Service
*s
, bool success
) {
937 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
]))
938 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
942 service_set_state(s
, SERVICE_STOP_POST
);
944 if (!s
->control_command
)
945 service_enter_dead(s
, true, true);
950 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s
)), strerror(-r
));
951 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
954 static void service_enter_signal(Service
*s
, ServiceState state
, bool success
) {
963 if (s
->main_pid
> 0 || s
->control_pid
> 0) {
966 sig
= (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
969 if (s
->main_pid
> 0) {
970 if (kill(s
->main_pid
, sig
) < 0 && errno
!= ESRCH
)
976 if (s
->control_pid
> 0) {
977 if (kill(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
987 service_set_state(s
, state
);
989 if (s
->main_pid
<= 0 && s
->control_pid
<= 0)
990 service_enter_dead(s
, true, true);
995 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s
)), strerror(-r
));
999 service_set_state(s
, state
);
1000 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
)
1001 service_enter_stop_post(s
, false);
1003 service_enter_dead(s
, false, true);
1006 static void service_enter_stop(Service
*s
, bool success
) {
1013 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
]))
1014 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
1017 service_set_state(s
, SERVICE_STOP
);
1019 if (!s
->control_command
)
1020 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, true);
1025 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1026 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1029 static void service_enter_start_post(Service
*s
) {
1033 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
]))
1034 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
1038 service_set_state(s
, SERVICE_START_POST
);
1040 if (!s
->control_command
)
1041 service_set_state(s
, SERVICE_RUNNING
);
1046 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1047 service_enter_stop(s
, false);
1050 static void service_enter_start(Service
*s
) {
1056 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1057 assert(!s
->exec_command
[SERVICE_EXEC_START
]->command_next
);
1059 if ((r
= service_spawn(s
, s
->exec_command
[SERVICE_EXEC_START
], s
->type
== SERVICE_FORKING
, true, &pid
)) < 0)
1062 service_set_state(s
, SERVICE_START
);
1064 if (s
->type
== SERVICE_SIMPLE
) {
1065 /* For simple services we immediately start
1066 * the START_POST binaries. */
1069 s
->main_pid_known
= true;
1070 service_enter_start_post(s
);
1072 } else if (s
->type
== SERVICE_FORKING
) {
1074 /* For forking services we wait until the start
1075 * process exited. */
1077 s
->control_pid
= pid
;
1078 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1079 } else if (s
->type
== SERVICE_FINISH
) {
1081 /* For finishing services we wait until the start
1082 * process exited, too, but it is our main process. */
1085 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1087 assert_not_reached("Unknown service type");
1092 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s
)), strerror(-r
));
1093 service_enter_stop(s
, false);
1096 static void service_enter_start_pre(Service
*s
) {
1101 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
]))
1102 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
1105 service_set_state(s
, SERVICE_START_PRE
);
1107 if (!s
->control_command
)
1108 service_enter_start(s
);
1113 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1114 service_enter_dead(s
, false, true);
1117 static void service_enter_restart(Service
*s
) {
1121 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
), JOB_FAIL
, false, NULL
)) < 0)
1124 log_debug("%s scheduled restart job.", unit_id(UNIT(s
)));
1125 service_enter_dead(s
, true, false);
1130 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s
)), strerror(-r
));
1131 service_enter_dead(s
, false, false);
1134 static void service_enter_reload(Service
*s
) {
1139 if ((s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
]))
1140 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
1143 service_set_state(s
, SERVICE_RELOAD
);
1145 if (!s
->control_command
)
1146 service_set_state(s
, SERVICE_RUNNING
);
1151 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1152 service_enter_stop(s
, false);
1155 static void service_run_next(Service
*s
, bool success
) {
1159 assert(s
->control_command
);
1160 assert(s
->control_command
->command_next
);
1165 s
->control_command
= s
->control_command
->command_next
;
1167 if ((r
= service_spawn(s
, s
->control_command
, true, false, &s
->control_pid
)) < 0)
1173 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s
)), strerror(-r
));
1175 if (s
->state
== SERVICE_STOP
)
1176 service_enter_stop_post(s
, false);
1177 else if (s
->state
== SERVICE_STOP_POST
)
1178 service_enter_dead(s
, false, true);
1180 service_enter_stop(s
, false);
1183 static int service_start(Unit
*u
) {
1184 Service
*s
= SERVICE(u
);
1188 /* We cannot fulfill this request right now, try again later
1190 if (s
->state
== SERVICE_STOP
||
1191 s
->state
== SERVICE_STOP_SIGTERM
||
1192 s
->state
== SERVICE_STOP_SIGKILL
||
1193 s
->state
== SERVICE_STOP_POST
||
1194 s
->state
== SERVICE_FINAL_SIGTERM
||
1195 s
->state
== SERVICE_FINAL_SIGKILL
)
1198 /* Already on it! */
1199 if (s
->state
== SERVICE_START_PRE
||
1200 s
->state
== SERVICE_START
||
1201 s
->state
== SERVICE_START_POST
)
1204 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_MAINTAINANCE
|| s
->state
== SERVICE_AUTO_RESTART
);
1206 /* Make sure we don't enter a busy loop of some kind. */
1207 if (!ratelimit_test(&s
->ratelimit
)) {
1208 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u
));
1213 s
->main_pid_known
= false;
1215 service_enter_start_pre(s
);
1219 static int service_stop(Unit
*u
) {
1220 Service
*s
= SERVICE(u
);
1224 if (s
->state
== SERVICE_START_PRE
||
1225 s
->state
== SERVICE_START
||
1226 s
->state
== SERVICE_START_POST
||
1227 s
->state
== SERVICE_RELOAD
)
1230 if (s
->state
== SERVICE_AUTO_RESTART
) {
1231 service_set_state(s
, SERVICE_DEAD
);
1235 assert(s
->state
== SERVICE_RUNNING
);
1237 service_enter_stop(s
, true);
1241 static int service_reload(Unit
*u
) {
1242 Service
*s
= SERVICE(u
);
1246 assert(s
->state
== SERVICE_RUNNING
);
1248 service_enter_reload(s
);
1252 static bool service_can_reload(Unit
*u
) {
1253 Service
*s
= SERVICE(u
);
1257 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1260 static UnitActiveState
service_active_state(Unit
*u
) {
1263 return state_translation_table
[SERVICE(u
)->state
];
1266 static int main_pid_good(Service
*s
) {
1269 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1272 /* If we know the pid file, then lets just check if it is
1274 if (s
->main_pid_known
)
1275 return s
->main_pid
> 0;
1277 /* We don't know the pid */
1281 static bool control_pid_good(Service
*s
) {
1284 return s
->control_pid
> 0;
1287 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1288 Service
*s
= SERVICE(u
);
1294 success
= code
== CLD_EXITED
&& status
== 0;
1295 s
->failure
= s
->failure
|| !success
;
1297 if (s
->main_pid
== pid
) {
1299 exec_status_fill(&s
->main_exec_status
, pid
, code
, status
);
1302 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_FINISH
) {
1303 assert(s
->exec_command
[SERVICE_EXEC_START
]);
1304 s
->exec_command
[SERVICE_EXEC_START
]->exec_status
= s
->main_exec_status
;
1307 log_debug("%s: main process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
1309 /* The service exited, so the service is officially
1314 case SERVICE_START_POST
:
1315 case SERVICE_RELOAD
:
1317 /* Need to wait until the operation is
1322 assert(s
->type
== SERVICE_FINISH
);
1324 /* This was our main goal, so let's go on */
1326 service_enter_start_post(s
);
1328 service_enter_stop(s
, false);
1331 case SERVICE_RUNNING
:
1332 service_enter_stop(s
, success
);
1335 case SERVICE_STOP_SIGTERM
:
1336 case SERVICE_STOP_SIGKILL
:
1338 if (!control_pid_good(s
))
1339 service_enter_stop_post(s
, success
);
1341 /* If there is still a control process, wait for that first */
1345 assert_not_reached("Uh, main process died at wrong time.");
1348 } else if (s
->control_pid
== pid
) {
1349 assert(s
->control_command
);
1351 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1354 log_debug("%s: control process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
1356 /* If we are shutting things down anyway we
1357 * don't care about failing commands. */
1359 if (s
->control_command
->command_next
&&
1360 (success
|| (s
->state
== SERVICE_EXEC_STOP
|| s
->state
== SERVICE_EXEC_STOP_POST
)))
1362 /* There is another command to *
1363 * execute, so let's do that. */
1365 service_run_next(s
, success
);
1368 /* No further commands for this step, so let's
1369 * figure out what to do next */
1371 log_debug("%s got final SIGCHLD for state %s", unit_id(u
), service_state_to_string(s
->state
));
1375 case SERVICE_START_PRE
:
1377 service_enter_start(s
);
1379 service_enter_stop(s
, false);
1383 assert(s
->type
== SERVICE_FORKING
);
1385 /* Let's try to load the pid
1386 * file here if we can. We
1387 * ignore the return value,
1388 * since the PID file might
1389 * actually be created by a
1390 * START_POST script */
1394 service_load_pid_file(s
);
1396 service_enter_start_post(s
);
1398 service_enter_stop(s
, false);
1402 case SERVICE_START_POST
:
1403 if (success
&& s
->pid_file
&& !s
->main_pid_known
) {
1406 /* Hmm, let's see if we can
1407 * load the pid now after the
1408 * start-post scripts got
1411 if ((r
= service_load_pid_file(s
)) < 0)
1412 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s
)), s
->pid_file
, strerror(-r
));
1417 case SERVICE_RELOAD
:
1419 if (main_pid_good(s
) != 0)
1420 service_set_state(s
, SERVICE_RUNNING
);
1422 service_enter_stop(s
, true);
1424 service_enter_stop(s
, false);
1429 if (main_pid_good(s
) > 0)
1430 /* Still not dead and we know the PID? Let's go hunting. */
1431 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, success
);
1433 service_enter_stop_post(s
, success
);
1436 case SERVICE_STOP_SIGTERM
:
1437 case SERVICE_STOP_SIGKILL
:
1438 if (main_pid_good(s
) <= 0)
1439 service_enter_stop_post(s
, success
);
1441 /* If there is still a service
1442 * process around, wait until
1443 * that one quit, too */
1446 case SERVICE_STOP_POST
:
1447 case SERVICE_FINAL_SIGTERM
:
1448 case SERVICE_FINAL_SIGKILL
:
1449 service_enter_dead(s
, success
, true);
1453 assert_not_reached("Uh, control process died at wrong time.");
1457 assert_not_reached("Got SIGCHLD for unkown PID");
1460 static void service_timer_event(Unit
*u
, uint64_t elapsed
, Watch
* w
) {
1461 Service
*s
= SERVICE(u
);
1464 assert(elapsed
== 1);
1466 assert(w
== &s
->timer_watch
);
1470 case SERVICE_START_PRE
:
1472 case SERVICE_START_POST
:
1473 case SERVICE_RELOAD
:
1474 log_warning("%s operation timed out. Stopping.", unit_id(u
));
1475 service_enter_stop(s
, false);
1479 log_warning("%s stopping timed out. Terminating.", unit_id(u
));
1480 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, false);
1483 case SERVICE_STOP_SIGTERM
:
1484 log_warning("%s stopping timed out. Killing.", unit_id(u
));
1485 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, false);
1488 case SERVICE_STOP_SIGKILL
:
1489 /* Uh, wie sent a SIGKILL and it is still not gone?
1490 * Must be something we cannot kill, so let's just be
1491 * weirded out and continue */
1493 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u
));
1494 service_enter_stop_post(s
, false);
1497 case SERVICE_STOP_POST
:
1498 log_warning("%s stopping timed out (2). Terminating.", unit_id(u
));
1499 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, false);
1502 case SERVICE_FINAL_SIGTERM
:
1503 log_warning("%s stopping timed out (2). Killing.", unit_id(u
));
1504 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, false);
1507 case SERVICE_FINAL_SIGKILL
:
1508 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u
));
1509 service_enter_dead(s
, false, true);
1512 case SERVICE_AUTO_RESTART
:
1513 log_debug("%s holdoff time over, scheduling restart.", unit_id(u
));
1514 service_enter_restart(s
);
1518 assert_not_reached("Timeout at wrong time.");
1522 static int service_enumerate(Manager
*m
) {
1524 static const char * const rcnd
[] = {
1525 "../rc0.d", SPECIAL_RUNLEVEL0_TARGET
,
1526 "../rc1.d", SPECIAL_RUNLEVEL1_TARGET
,
1527 "../rc2.d", SPECIAL_RUNLEVEL2_TARGET
,
1528 "../rc3.d", SPECIAL_RUNLEVEL3_TARGET
,
1529 "../rc4.d", SPECIAL_RUNLEVEL4_TARGET
,
1530 "../rc5.d", SPECIAL_RUNLEVEL5_TARGET
,
1531 "../rc6.d", SPECIAL_RUNLEVEL6_TARGET
1537 char *path
= NULL
, *fpath
= NULL
, *name
= NULL
;
1542 STRV_FOREACH(p
, m
->sysvinit_path
)
1543 for (i
= 0; i
< ELEMENTSOF(rcnd
); i
+= 2) {
1548 if (asprintf(&path
, "%s/%s", *p
, rcnd
[i
]) < 0) {
1556 if (!(d
= opendir(path
))) {
1557 if (errno
!= ENOENT
)
1558 log_warning("opendir() failed on %s: %s", path
, strerror(errno
));
1563 while ((de
= readdir(d
))) {
1564 Unit
*runlevel
, *service
;
1566 if (ignore_file(de
->d_name
))
1569 if (de
->d_name
[0] != 'S' && de
->d_name
[0] != 'K')
1572 if (strlen(de
->d_name
) < 4)
1577 if (asprintf(&fpath
, "%s/%s/%s", *p
, rcnd
[i
], de
->d_name
) < 0) {
1582 if (access(fpath
, X_OK
) < 0) {
1584 if (errno
!= ENOENT
)
1585 log_warning("access() failed on %s: %s", fpath
, strerror(errno
));
1592 if (asprintf(&name
, "%s.service", de
->d_name
+3) < 0) {
1597 if ((r
= manager_load_unit(m
, name
, &service
)) < 0)
1600 /* Don't allow that non-SysV services
1601 * are started via rcN.d/ links. */
1602 if (!SERVICE(service
)->sysv_path
)
1605 if ((r
= manager_load_unit(m
, rcnd
[i
+1], &runlevel
)) < 0)
1608 if (de
->d_name
[0] == 'S') {
1609 if ((r
= unit_add_dependency(runlevel
, UNIT_WANTS
, service
)) < 0)
1612 if ((r
= unit_add_dependency(runlevel
, UNIT_AFTER
, service
)) < 0)
1615 if ((r
= unit_add_dependency(runlevel
, UNIT_CONFLICTS
, service
)) < 0)
1618 if ((r
= unit_add_dependency(runlevel
, UNIT_BEFORE
, service
)) < 0)
1635 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
1636 [SERVICE_DEAD
] = "dead",
1637 [SERVICE_START_PRE
] = "start-pre",
1638 [SERVICE_START
] = "start",
1639 [SERVICE_START_POST
] = "start-post",
1640 [SERVICE_RUNNING
] = "running",
1641 [SERVICE_RELOAD
] = "reload",
1642 [SERVICE_STOP
] = "stop",
1643 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
1644 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
1645 [SERVICE_STOP_POST
] = "stop-post",
1646 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
1647 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
1648 [SERVICE_MAINTAINANCE
] = "maintainance",
1649 [SERVICE_AUTO_RESTART
] = "auto-restart",
1652 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
1654 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
1655 [SERVICE_ONCE
] = "once",
1656 [SERVICE_RESTART_ON_SUCCESS
] = "restart-on-success",
1657 [SERVICE_RESTART_ALWAYS
] = "restart-on-failure",
1660 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
1662 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
1663 [SERVICE_FORKING
] = "forking",
1664 [SERVICE_SIMPLE
] = "simple",
1665 [SERVICE_FINISH
] = "finish"
1668 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
1670 static const char* const service_exec_command_table
[_SERVICE_EXEC_MAX
] = {
1671 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
1672 [SERVICE_EXEC_START
] = "ExecStart",
1673 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
1674 [SERVICE_EXEC_RELOAD
] = "ExecReload",
1675 [SERVICE_EXEC_STOP
] = "ExecStop",
1676 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
1679 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
1681 const UnitVTable service_vtable
= {
1682 .suffix
= ".service",
1684 .init
= service_init
,
1685 .done
= service_done
,
1687 .dump
= service_dump
,
1689 .start
= service_start
,
1690 .stop
= service_stop
,
1691 .reload
= service_reload
,
1693 .can_reload
= service_can_reload
,
1695 .active_state
= service_active_state
,
1697 .sigchld_event
= service_sigchld_event
,
1698 .timer_event
= service_timer_event
,
1700 .enumerate
= service_enumerate