1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
8 #include "load-fragment.h"
9 #include "load-dropin.h"
12 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
13 [SERVICE_DEAD] = UNIT_INACTIVE,
14 [SERVICE_START_PRE] = UNIT_ACTIVATING,
15 [SERVICE_START] = UNIT_ACTIVATING,
16 [SERVICE_START_POST] = UNIT_ACTIVATING,
17 [SERVICE_RUNNING] = UNIT_ACTIVE,
18 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
19 [SERVICE_STOP] = UNIT_DEACTIVATING,
20 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
21 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
22 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
23 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
24 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
25 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
26 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
29 static const char* const state_string_table[_SERVICE_STATE_MAX] = {
30 [SERVICE_DEAD] = "dead",
31 [SERVICE_START_PRE] = "start-pre",
32 [SERVICE_START] = "start",
33 [SERVICE_START_POST] = "post",
34 [SERVICE_RUNNING] = "running",
35 [SERVICE_RELOAD] = "reload",
36 [SERVICE_STOP] = "stop",
37 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
38 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
39 [SERVICE_STOP_POST] = "stop-post",
40 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
41 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
42 [SERVICE_MAINTAINANCE] = "maintainance",
43 [SERVICE_AUTO_RESTART] = "auto-restart",
46 static void service_done(Unit *u) {
47 Service *s = SERVICE(u);
54 exec_context_done(&s->exec_context);
55 exec_command_free_array(s->exec_command, _SERVICE_EXEC_MAX);
56 s->control_command = NULL;
58 /* This will leak a process, but at least no memory or any of
60 if (s->main_pid > 0) {
61 unit_unwatch_pid(u, s->main_pid);
65 if (s->control_pid > 0) {
66 unit_unwatch_pid(u, s->control_pid);
70 unit_unwatch_timer(u, &s->timer_watch);
73 static int service_load_sysv(Service *s) {
76 /* Load service data from SysV init scripts, preferably with
82 static int service_init(Unit *u) {
84 Service *s = SERVICE(u);
88 /* First, reset everything to the defaults, in case this is a
94 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
95 s->restart_usec = DEFAULT_RESTART_USEC;
97 exec_context_init(&s->exec_context);
99 s->timer_watch.type = WATCH_INVALID;
101 s->state = SERVICE_DEAD;
103 /* Load a .service file */
104 r = unit_load_fragment(u);
106 /* Load a classic init script as a fallback */
108 r = service_load_sysv(s);
115 /* Load dropin directory data */
116 if ((r = unit_load_dropin(u)) < 0) {
124 static void service_dump(Unit *u, FILE *f, const char *prefix) {
126 static const char* const command_table[_SERVICE_EXEC_MAX] = {
127 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
128 [SERVICE_EXEC_START] = "ExecStart",
129 [SERVICE_EXEC_START_POST] = "ExecStartPost",
130 [SERVICE_EXEC_RELOAD] = "ExecReload",
131 [SERVICE_EXEC_STOP] = "ExecStop",
132 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
135 ServiceExecCommand c;
136 Service *s = SERVICE(u);
141 prefix2 = strappend(prefix, "\t");
146 "%sService State: %s\n",
147 prefix, state_string_table[s->state]);
152 prefix, s->pid_file);
155 exec_context_dump(&s->exec_context, f, prefix);
157 for (c = 0; c < _SERVICE_EXEC_MAX; c++) {
159 if (!s->exec_command[c])
162 fprintf(f, "%s→ %s:\n",
163 prefix, command_table[c]);
165 exec_command_dump_list(s->exec_command[c], f, prefix2);
171 static int service_load_pid_file(Service *s) {
178 if (s->main_pid_known)
184 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
187 if ((r = safe_atolu(k, &p)) < 0) {
192 if ((unsigned long) (pid_t) p != p)
196 s->main_pid_known = true;
201 static void service_set_state(Service *s, ServiceState state) {
202 ServiceState old_state;
205 old_state = s->state;
208 if (state != SERVICE_START_PRE &&
209 state != SERVICE_START &&
210 state != SERVICE_START_POST &&
211 state != SERVICE_RELOAD &&
212 state != SERVICE_STOP &&
213 state != SERVICE_STOP_SIGTERM &&
214 state != SERVICE_STOP_SIGKILL &&
215 state != SERVICE_STOP_POST &&
216 state != SERVICE_FINAL_SIGTERM &&
217 state != SERVICE_FINAL_SIGKILL &&
218 state != SERVICE_AUTO_RESTART)
219 unit_unwatch_timer(UNIT(s), &s->timer_watch);
221 if (state != SERVICE_START_POST &&
222 state != SERVICE_RUNNING &&
223 state != SERVICE_RELOAD &&
224 state != SERVICE_STOP &&
225 state != SERVICE_STOP_SIGTERM &&
226 state != SERVICE_STOP_SIGKILL)
227 if (s->main_pid > 0) {
228 unit_unwatch_pid(UNIT(s), s->main_pid);
232 if (state != SERVICE_START_PRE &&
233 state != SERVICE_START &&
234 state != SERVICE_START_POST &&
235 state != SERVICE_RELOAD &&
236 state != SERVICE_STOP &&
237 state != SERVICE_STOP_SIGTERM &&
238 state != SERVICE_STOP_SIGKILL &&
239 state != SERVICE_STOP_POST &&
240 state != SERVICE_FINAL_SIGTERM &&
241 state != SERVICE_FINAL_SIGKILL)
242 if (s->control_pid > 0) {
243 unit_unwatch_pid(UNIT(s), s->control_pid);
247 if (state != SERVICE_START_PRE &&
248 state != SERVICE_START &&
249 state != SERVICE_START_POST &&
250 state != SERVICE_RELOAD &&
251 state != SERVICE_STOP &&
252 state != SERVICE_STOP_POST)
253 s->control_command = NULL;
255 log_debug("%s changing %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
257 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
260 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
271 SET_FOREACH(t, UNIT(s)->meta.names, i) {
277 /* Look for all socket objects that go by any of our
278 * units and collect their fds */
280 if (!(k = unit_name_change_suffix(t, ".socket"))) {
285 p = manager_get_unit(UNIT(s)->meta.manager, k);
291 if ((r = socket_collect_fds(SOCKET(p), &cfds, &cn_fds)) < 0)
303 if (!(t = new(int, rn_fds+cn_fds))) {
309 memcpy(t, rfds, rn_fds);
310 memcpy(t+rn_fds, cfds, cn_fds);
315 rn_fds = rn_fds+cn_fds;
328 static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds, pid_t *_pid) {
339 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
343 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
346 unit_unwatch_timer(UNIT(s), &s->timer_watch);
348 if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
351 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
352 /* FIXME: we need to do something here */
364 unit_unwatch_timer(UNIT(s), &s->timer_watch);
369 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
377 (s->restart == SERVICE_RESTART_ALWAYS ||
378 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
380 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
383 service_set_state(s, SERVICE_AUTO_RESTART);
385 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
390 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
391 service_enter_dead(s, false, false);
394 static void service_enter_signal(Service *s, ServiceState state, bool success);
396 static void service_enter_stop_post(Service *s, bool success) {
403 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
405 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
408 service_set_state(s, SERVICE_STOP_POST);
410 service_enter_dead(s, true, true);
415 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
416 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
419 static void service_enter_signal(Service *s, ServiceState state, bool success) {
428 if (s->main_pid > 0 || s->control_pid > 0) {
431 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
434 if (s->main_pid > 0) {
435 if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
441 if (s->control_pid > 0) {
442 if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
451 service_set_state(s, state);
453 service_enter_dead(s, true, true);
458 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
462 service_set_state(s, state);
463 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
464 service_enter_stop_post(s, false);
466 service_enter_dead(s, false, true);
469 static void service_enter_stop(Service *s, bool success) {
476 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
478 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
481 service_set_state(s, SERVICE_STOP);
483 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
488 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
489 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
492 static void service_enter_start_post(Service *s) {
496 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
498 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
501 service_set_state(s, SERVICE_START_POST);
503 service_set_state(s, SERVICE_RUNNING);
508 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
509 service_enter_stop(s, false);
512 static void service_enter_start(Service *s) {
518 assert(s->exec_command[SERVICE_EXEC_START]);
519 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
521 if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
524 if (s->type == SERVICE_SIMPLE) {
525 /* For simple services we immediately start
526 * the START_POST binaries. */
529 s->main_pid_known = true;
530 service_enter_start_post(s);
532 } else if (s->type == SERVICE_FORKING) {
534 /* For forking services we wait until the start
537 s->control_pid = pid;
538 s->control_command = s->exec_command[SERVICE_EXEC_START];
539 service_set_state(s, SERVICE_START);
541 assert_not_reached("Unknown service type");
546 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
547 service_enter_stop(s, false);
550 static void service_enter_start_pre(Service *s) {
555 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
557 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
560 service_set_state(s, SERVICE_START_PRE);
562 service_enter_start(s);
567 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
568 service_enter_dead(s, false, true);
571 static void service_enter_restart(Service *s) {
575 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
578 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
579 service_enter_dead(s, true, false);
584 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
585 service_enter_dead(s, false, false);
588 static void service_enter_reload(Service *s) {
593 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
595 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
598 service_set_state(s, SERVICE_RELOAD);
600 service_set_state(s, SERVICE_RUNNING);
605 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
606 service_enter_stop(s, false);
609 static void service_run_next(Service *s, bool success) {
613 assert(s->control_command);
614 assert(s->control_command->command_next);
619 s->control_command = s->control_command->command_next;
621 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
627 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
629 if (s->state == SERVICE_STOP)
630 service_enter_stop_post(s, false);
631 else if (s->state == SERVICE_STOP_POST)
632 service_enter_dead(s, false, true);
634 service_enter_stop(s, false);
637 static int service_start(Unit *u) {
638 Service *s = SERVICE(u);
642 /* We cannot fulfill this request right now, try again later
644 if (s->state == SERVICE_STOP ||
645 s->state == SERVICE_STOP_SIGTERM ||
646 s->state == SERVICE_STOP_SIGKILL ||
647 s->state == SERVICE_STOP_POST ||
648 s->state == SERVICE_FINAL_SIGTERM ||
649 s->state == SERVICE_FINAL_SIGKILL)
653 if (s->state == SERVICE_START_PRE ||
654 s->state == SERVICE_START ||
655 s->state == SERVICE_START_POST)
658 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
661 s->main_pid_known = false;
663 service_enter_start_pre(s);
667 static int service_stop(Unit *u) {
668 Service *s = SERVICE(u);
672 if (s->state == SERVICE_START_PRE ||
673 s->state == SERVICE_START ||
674 s->state == SERVICE_START_POST ||
675 s->state == SERVICE_RELOAD)
678 if (s->state == SERVICE_AUTO_RESTART) {
679 service_set_state(s, SERVICE_DEAD);
683 assert(s->state == SERVICE_RUNNING);
685 service_enter_stop(s, true);
689 static int service_reload(Unit *u) {
690 Service *s = SERVICE(u);
694 assert(s->state == SERVICE_RUNNING);
696 service_enter_reload(s);
700 static bool service_can_reload(Unit *u) {
701 Service *s = SERVICE(u);
705 return !!s->exec_command[SERVICE_EXEC_RELOAD];
708 static UnitActiveState service_active_state(Unit *u) {
711 return state_translation_table[SERVICE(u)->state];
714 static int main_pid_good(Service *s) {
717 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
720 /* If we know the pid file, then lets just check if it is
722 if (s->main_pid_known)
723 return s->main_pid > 0;
725 /* We don't know the pid */
729 static bool control_pid_good(Service *s) {
732 return s->control_pid > 0;
735 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
736 Service *s = SERVICE(u);
742 success = code == CLD_EXITED && status == 0;
743 s->failure = s->failure || !success;
745 if (s->main_pid == pid) {
747 exec_status_fill(&s->main_exec_status, pid, code, status);
750 if (s->type == SERVICE_SIMPLE) {
751 assert(s->exec_command[SERVICE_EXEC_START]);
752 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
755 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
757 /* The service exited, so the service is officially
762 case SERVICE_START_POST:
765 /* Need to wait until the operation is
769 case SERVICE_RUNNING:
770 service_enter_stop(s, success);
773 case SERVICE_STOP_SIGTERM:
774 case SERVICE_STOP_SIGKILL:
776 if (!control_pid_good(s))
777 service_enter_stop_post(s, success);
779 /* If there is still a control process, wait for that first */
783 assert_not_reached("Uh, main process died at wrong time.");
786 } else if (s->control_pid == pid) {
787 assert(s->control_command);
789 exec_status_fill(&s->control_command->exec_status, pid, code, status);
792 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
794 /* If we are shutting things down anyway we
795 * don't care about failing commands. */
797 if (s->control_command->command_next &&
798 (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
800 /* There is another command to *
801 * execute, so let's do that. */
803 service_run_next(s, success);
806 /* No further commands for this step, so let's
807 * figure out what to do next */
809 log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
813 case SERVICE_START_PRE:
815 service_enter_start(s);
817 service_enter_stop(s, false);
821 assert(s->type == SERVICE_FORKING);
823 /* Let's try to load the pid
824 * file here if we can. We
825 * ignore the return value,
826 * since the PID file might
827 * actually be created by a
828 * START_POST script */
832 service_load_pid_file(s);
834 service_enter_start_post(s);
836 service_enter_stop(s, false);
840 case SERVICE_START_POST:
841 if (success && s->pid_file && !s->main_pid_known) {
844 /* Hmm, let's see if we can
845 * load the pid now after the
846 * start-post scripts got
849 if ((r = service_load_pid_file(s)) < 0)
850 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
857 if (main_pid_good(s) != 0)
858 service_set_state(s, SERVICE_RUNNING);
860 service_enter_stop(s, true);
862 service_enter_stop(s, false);
867 if (main_pid_good(s) > 0)
868 /* Still not dead and we know the PID? Let's go hunting. */
869 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
871 service_enter_stop_post(s, success);
874 case SERVICE_STOP_SIGTERM:
875 case SERVICE_STOP_SIGKILL:
876 if (main_pid_good(s) <= 0)
877 service_enter_stop_post(s, success);
879 /* If there is still a service
880 * process around, wait until
881 * that one quit, too */
884 case SERVICE_STOP_POST:
885 case SERVICE_FINAL_SIGTERM:
886 case SERVICE_FINAL_SIGKILL:
887 service_enter_dead(s, success, true);
891 assert_not_reached("Uh, control process died at wrong time.");
895 assert_not_reached("Got SIGCHLD for unkown PID");
898 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
899 Service *s = SERVICE(u);
902 assert(elapsed == 1);
904 assert(w == &s->timer_watch);
908 case SERVICE_START_PRE:
910 case SERVICE_START_POST:
912 log_warning("%s operation timed out. Stopping.", unit_id(u));
913 service_enter_stop(s, false);
917 log_warning("%s stopping timed out. Terminating.", unit_id(u));
918 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
921 case SERVICE_STOP_SIGTERM:
922 log_warning("%s stopping timed out. Killing.", unit_id(u));
923 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
926 case SERVICE_STOP_SIGKILL:
927 /* Uh, wie sent a SIGKILL and it is still not gone?
928 * Must be something we cannot kill, so let's just be
929 * weirded out and continue */
931 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
932 service_enter_stop_post(s, false);
935 case SERVICE_STOP_POST:
936 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
937 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
940 case SERVICE_FINAL_SIGTERM:
941 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
942 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
945 case SERVICE_FINAL_SIGKILL:
946 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
947 service_enter_dead(s, false, true);
950 case SERVICE_AUTO_RESTART:
951 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
952 service_enter_restart(s);
956 assert_not_reached("Timeout at wrong time.");
960 const UnitVTable service_vtable = {
961 .suffix = ".service",
963 .init = service_init,
964 .done = service_done,
966 .dump = service_dump,
968 .start = service_start,
969 .stop = service_stop,
970 .reload = service_reload,
972 .can_reload = service_can_reload,
974 .active_state = service_active_state,
976 .sigchld_event = service_sigchld_event,
977 .timer_event = service_timer_event,