1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
8 #include "load-fragment.h"
9 #include "load-dropin.h"
12 static const NameActiveState state_table[_SERVICE_STATE_MAX] = {
13 [SERVICE_DEAD] = NAME_INACTIVE,
14 [SERVICE_START_PRE] = NAME_ACTIVATING,
15 [SERVICE_START] = NAME_ACTIVATING,
16 [SERVICE_START_POST] = NAME_ACTIVATING,
17 [SERVICE_RUNNING] = NAME_ACTIVE,
18 [SERVICE_RELOAD] = NAME_ACTIVE_RELOADING,
19 [SERVICE_STOP] = NAME_DEACTIVATING,
20 [SERVICE_STOP_SIGTERM] = NAME_DEACTIVATING,
21 [SERVICE_STOP_SIGKILL] = NAME_DEACTIVATING,
22 [SERVICE_STOP_POST] = NAME_DEACTIVATING,
23 [SERVICE_FINAL_SIGTERM] = NAME_DEACTIVATING,
24 [SERVICE_FINAL_SIGKILL] = NAME_DEACTIVATING,
25 [SERVICE_MAINTAINANCE] = NAME_INACTIVE,
26 [SERVICE_AUTO_RESTART] = NAME_ACTIVATING,
29 static int service_load_sysv(Service *s) {
32 /* Load service data from SysV init scripts, preferably with
38 static int service_init(Name *n) {
40 Service *s = SERVICE(n);
44 /* First, reset everything to the defaults, in case this is a
50 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
51 s->restart_usec = DEFAULT_RESTART_USEC;
53 exec_context_init(&s->exec_context);
57 s->state = SERVICE_DEAD;
59 /* Load a .service file */
60 r = name_load_fragment(n);
62 /* Load a classic init script as a fallback */
64 r = service_load_sysv(s);
69 /* Load dropin directory data */
70 if ((r = name_load_dropin(n)) < 0)
76 static void service_done(Name *n) {
77 Service *s = SERVICE(n);
84 exec_context_done(&s->exec_context);
85 exec_command_free_array(s->exec_command, _SERVICE_EXEC_MAX);
86 s->control_command = NULL;
88 /* This will leak a process, but at least no memory or any of
90 if (s->main_pid > 0) {
91 name_unwatch_pid(n, s->main_pid);
95 if (s->control_pid > 0) {
96 name_unwatch_pid(n, s->control_pid);
100 name_unwatch_timer(n, &s->timer_id);
103 static void service_dump(Name *n, FILE *f, const char *prefix) {
105 static const char* const state_table[_SERVICE_STATE_MAX] = {
106 [SERVICE_DEAD] = "dead",
107 [SERVICE_START_PRE] = "start-pre",
108 [SERVICE_START] = "start",
109 [SERVICE_START_POST] = "post",
110 [SERVICE_RUNNING] = "running",
111 [SERVICE_RELOAD] = "reload",
112 [SERVICE_STOP] = "stop",
113 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
114 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
115 [SERVICE_STOP_POST] = "stop-post",
116 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
117 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
118 [SERVICE_MAINTAINANCE] = "maintainance",
119 [SERVICE_AUTO_RESTART] = "auto-restart",
122 static const char* const command_table[_SERVICE_EXEC_MAX] = {
123 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
124 [SERVICE_EXEC_START] = "ExecStart",
125 [SERVICE_EXEC_START_POST] = "ExecStartPost",
126 [SERVICE_EXEC_RELOAD] = "ExecReload",
127 [SERVICE_EXEC_STOP] = "ExecStop",
128 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
131 ServiceExecCommand c;
132 Service *s = SERVICE(n);
137 "%sService State: %s\n",
138 prefix, state_table[s->state]);
143 prefix, s->pid_file);
146 exec_context_dump(&s->exec_context, f, prefix);
148 for (c = 0; c < _SERVICE_EXEC_MAX; c++) {
151 LIST_FOREACH(command, i, s->exec_command[c])
152 fprintf(f, "%s%s: %s\n", prefix, command_table[c], i->path);
156 static int service_load_pid_file(Service *s) {
163 if (s->main_pid_known)
169 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
172 if ((r = safe_atolu(k, &p)) < 0) {
177 if ((unsigned long) (pid_t) p != p)
181 s->main_pid_known = true;
186 static void service_set_state(Service *s, ServiceState state) {
187 ServiceState old_state;
190 old_state = s->state;
193 if (state != SERVICE_START_PRE &&
194 state != SERVICE_START &&
195 state != SERVICE_START_POST &&
196 state != SERVICE_RELOAD &&
197 state != SERVICE_STOP &&
198 state != SERVICE_STOP_SIGTERM &&
199 state != SERVICE_STOP_SIGKILL &&
200 state != SERVICE_STOP_POST &&
201 state != SERVICE_FINAL_SIGTERM &&
202 state != SERVICE_FINAL_SIGKILL &&
203 state != SERVICE_AUTO_RESTART)
204 name_unwatch_timer(NAME(s), &s->timer_id);
206 if (state != SERVICE_START_POST &&
207 state != SERVICE_RUNNING &&
208 state != SERVICE_RELOAD &&
209 state != SERVICE_STOP &&
210 state != SERVICE_STOP_SIGTERM &&
211 state != SERVICE_STOP_SIGKILL)
212 if (s->main_pid >= 0) {
213 name_unwatch_pid(NAME(s), s->main_pid);
217 if (state != SERVICE_START_PRE &&
218 state != SERVICE_START &&
219 state != SERVICE_START_POST &&
220 state != SERVICE_RELOAD &&
221 state != SERVICE_STOP &&
222 state != SERVICE_STOP_SIGTERM &&
223 state != SERVICE_STOP_SIGKILL &&
224 state != SERVICE_STOP_POST &&
225 state != SERVICE_FINAL_SIGTERM &&
226 state != SERVICE_FINAL_SIGKILL)
227 if (s->control_pid >= 0) {
228 name_unwatch_pid(NAME(s), s->control_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_POST)
238 s->control_command = NULL;
240 name_notify(NAME(s), state_table[old_state], state_table[s->state]);
243 static int service_spawn(Service *s, ExecCommand *c, bool timeout, pid_t *_pid) {
252 if ((r = name_watch_timer(NAME(s), s->timeout_usec, &s->timer_id)) < 0)
255 name_unwatch_timer(NAME(s), &s->timer_id);
257 if ((r = exec_spawn(c, &s->exec_context, NULL, 0, &pid)) < 0)
260 if ((r = name_watch_pid(NAME(s), pid)) < 0)
261 /* FIXME: we need to do something here */
270 name_unwatch_timer(NAME(s), &s->timer_id);
275 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
283 (s->restart == SERVICE_RESTART_ALWAYS ||
284 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
286 if ((r = name_watch_timer(NAME(s), s->restart_usec, &s->timer_id)) < 0)
289 service_set_state(s, SERVICE_AUTO_RESTART);
291 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
296 log_warning("%s failed to run install restart timer: %s", name_id(NAME(s)), strerror(-r));
297 service_enter_dead(s, false, false);
300 static void service_enter_signal(Service *s, ServiceState state, bool success);
302 static void service_enter_stop_post(Service *s, bool success) {
309 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
311 if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
314 service_set_state(s, SERVICE_STOP_POST);
316 service_enter_dead(s, true, true);
321 log_warning("%s failed to run stop executable: %s", name_id(NAME(s)), strerror(-r));
322 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
325 static void service_enter_signal(Service *s, ServiceState state, bool success) {
334 if (s->main_pid > 0 || s->control_pid > 0) {
337 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
340 if (s->main_pid > 0) {
341 if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
347 if (s->control_pid > 0) {
348 if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
357 service_set_state(s, state);
359 service_enter_dead(s, true, true);
364 log_warning("%s failed to kill processes: %s", name_id(NAME(s)), strerror(-r));
368 service_set_state(s, state);
369 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
370 service_enter_stop_post(s, false);
372 service_enter_dead(s, false, true);
375 static void service_enter_stop(Service *s, bool success) {
382 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
384 if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
387 service_set_state(s, SERVICE_STOP);
389 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
394 log_warning("%s failed to run stop executable: %s", name_id(NAME(s)), strerror(-r));
395 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
398 static void service_enter_start_post(Service *s) {
402 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
404 if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
407 service_set_state(s, SERVICE_START_POST);
409 service_set_state(s, SERVICE_RUNNING);
414 log_warning("%s failed to run start-post executable: %s", name_id(NAME(s)), strerror(-r));
415 service_enter_stop(s, false);
418 static void service_enter_start(Service *s) {
424 assert(s->exec_command[SERVICE_EXEC_START]);
425 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
427 if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, &pid)) < 0)
430 if (s->type == SERVICE_SIMPLE) {
431 /* For simple services we immediately start
432 * the START_POST binaries. */
435 s->main_pid_known = true;
436 service_enter_start_post(s);
438 } else if (s->type == SERVICE_FORKING) {
440 /* For forking services we wait until the start
443 s->control_pid = pid;
444 s->control_command = s->exec_command[SERVICE_EXEC_START];
445 service_set_state(s, SERVICE_START);
447 assert_not_reached("Unknown service type");
452 log_warning("%s failed to run start exectuable: %s", name_id(NAME(s)), strerror(-r));
453 service_enter_stop(s, false);
456 static void service_enter_start_pre(Service *s) {
461 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
463 if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
466 service_set_state(s, SERVICE_START_PRE);
468 service_enter_start(s);
473 log_warning("%s failed to run start-pre executable: %s", name_id(NAME(s)), strerror(-r));
474 service_enter_dead(s, false, true);
477 static void service_enter_restart(Service *s) {
481 if ((r = manager_add_job(NAME(s)->meta.manager, JOB_START, NAME(s), JOB_FAIL, false, NULL)) < 0)
484 log_debug("%s scheduled restart job.", name_id(NAME(s)));
485 service_enter_dead(s, true, false);
490 log_warning("%s failed to schedule restart job: %s", name_id(NAME(s)), strerror(-r));
491 service_enter_dead(s, false, false);
494 static void service_enter_reload(Service *s) {
499 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
501 if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
504 service_set_state(s, SERVICE_RELOAD);
506 service_set_state(s, SERVICE_RUNNING);
511 log_warning("%s failed to run reload executable: %s", name_id(NAME(s)), strerror(-r));
512 service_enter_stop(s, false);
515 static void service_run_next(Service *s, bool success) {
519 assert(s->control_command);
520 assert(s->control_command->command_next);
525 s->control_command = s->control_command->command_next;
527 if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
533 log_warning("%s failed to run spawn next executable: %s", name_id(NAME(s)), strerror(-r));
535 if (s->state == SERVICE_STOP)
536 service_enter_stop_post(s, false);
537 else if (s->state == SERVICE_STOP_POST)
538 service_enter_dead(s, false, true);
540 service_enter_stop(s, false);
543 static int service_start(Name *n) {
544 Service *s = SERVICE(n);
548 /* We cannot fulfill this request right now, try again later
550 if (s->state == SERVICE_STOP ||
551 s->state == SERVICE_STOP_SIGTERM ||
552 s->state == SERVICE_STOP_SIGKILL ||
553 s->state == SERVICE_STOP_POST ||
554 s->state == SERVICE_FINAL_SIGTERM ||
555 s->state == SERVICE_FINAL_SIGKILL)
559 if (s->state == SERVICE_START_PRE ||
560 s->state == SERVICE_START ||
561 s->state == SERVICE_START_POST)
564 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
567 s->main_pid_known = false;
569 service_enter_start_pre(s);
573 static int service_stop(Name *n) {
574 Service *s = SERVICE(n);
578 if (s->state == SERVICE_START_PRE ||
579 s->state == SERVICE_START ||
580 s->state == SERVICE_START_POST ||
581 s->state == SERVICE_RELOAD)
584 if (s->state == SERVICE_AUTO_RESTART) {
585 service_set_state(s, SERVICE_DEAD);
589 assert(s->state == SERVICE_RUNNING);
591 service_enter_stop(s, true);
595 static int service_reload(Name *n) {
596 Service *s = SERVICE(n);
600 assert(s->state == SERVICE_RUNNING);
602 service_enter_reload(s);
606 static bool service_can_reload(Name *n) {
607 Service *s = SERVICE(n);
611 return !!s->exec_command[SERVICE_EXEC_RELOAD];
614 static NameActiveState service_active_state(Name *n) {
617 return state_table[SERVICE(n)->state];
620 static int main_pid_good(Service *s) {
623 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
626 /* If we know the pid file, then lets just check if it is
628 if (s->main_pid_known)
629 return s->main_pid > 0;
631 /* We don't know the pid */
635 static bool control_pid_good(Service *s) {
638 return s->control_pid > 0;
641 static void service_sigchld_event(Name *n, pid_t pid, int code, int status) {
642 Service *s = SERVICE(n);
648 success = code == CLD_EXITED || status == 0;
649 s->failure = s->failure || !success;
651 if (s->main_pid == pid) {
653 exec_status_fill(&s->main_exec_status, pid, code, status);
656 if (s->type == SERVICE_SIMPLE) {
657 assert(s->exec_command[SERVICE_EXEC_START]);
658 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
661 log_debug("%s: main process exited, code=%s status=%i", name_id(n), sigchld_code(code), status);
663 /* The service exited, so the service is officially
668 case SERVICE_START_POST:
671 /* Need to wait until the operation is
675 case SERVICE_RUNNING:
676 service_enter_stop(s, success);
679 case SERVICE_STOP_SIGTERM:
680 case SERVICE_STOP_SIGKILL:
682 if (!control_pid_good(s))
683 service_enter_stop_post(s, success);
685 /* If there is still a control process, wait for that first */
689 assert_not_reached("Uh, main process died at wrong time.");
692 } else if (s->control_pid == pid) {
693 assert(s->control_command);
695 exec_status_fill(&s->control_command->exec_status, pid, code, status);
698 log_debug("%s: control process exited, code=%s status=%i", name_id(n), sigchld_code(code), status);
700 /* If we are shutting things down anyway we
701 * don't care about failing commands. */
703 if (s->control_command->command_next &&
704 (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
706 /* There is another command to *
707 * execute, so let's do that. */
709 service_run_next(s, success);
712 /* No further commands for this step, so let's
713 * figure out what to do next */
717 case SERVICE_START_PRE:
719 service_enter_start(s);
721 service_enter_stop(s, false);
725 assert(s->type == SERVICE_FORKING);
727 /* Let's try to load the pid
728 * file here if we can. We
729 * ignore the return value,
730 * since the PID file might
731 * actually be created by a
732 * START_POST script */
736 service_load_pid_file(s);
738 service_enter_start_post(s);
740 service_enter_stop(s, false);
744 case SERVICE_START_POST:
745 if (success && s->pid_file && !s->main_pid_known) {
748 /* Hmm, let's see if we can
749 * load the pid now after the
750 * start-post scripts got
753 if ((r = service_load_pid_file(s)) < 0)
754 log_warning("%s: failed to load PID file %s: %s", name_id(NAME(s)), s->pid_file, strerror(-r));
761 if (main_pid_good(s) != 0)
762 service_set_state(s, SERVICE_RUNNING);
764 service_enter_stop(s, true);
766 service_enter_stop(s, false);
771 if (main_pid_good(s) > 0)
772 /* Still not dead and we know the PID? Let's go hunting. */
773 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
775 service_enter_stop_post(s, success);
778 case SERVICE_STOP_SIGTERM:
779 case SERVICE_STOP_SIGKILL:
780 if (main_pid_good(s) <= 0)
781 service_enter_stop_post(s, success);
783 /* If there is still a service
784 * process around, wait until
785 * that one quit, too */
788 case SERVICE_STOP_POST:
789 case SERVICE_FINAL_SIGTERM:
790 case SERVICE_FINAL_SIGKILL:
791 service_enter_dead(s, success, true);
795 assert_not_reached("Uh, control process died at wrong time.");
799 assert_not_reached("Got SIGCHLD for unkown PID");
802 static void service_timer_event(Name *n, int id, uint64_t elapsed) {
803 Service *s = SERVICE(n);
806 assert(elapsed == 1);
808 assert(s->timer_id == id);
812 case SERVICE_START_PRE:
814 case SERVICE_START_POST:
816 log_warning("%s operation timed out. Stopping.", name_id(n));
817 service_enter_stop(s, false);
821 log_warning("%s stopping timed out. Terminating.", name_id(n));
822 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
825 case SERVICE_STOP_SIGTERM:
826 log_warning("%s stopping timed out. Killing.", name_id(n));
827 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
830 case SERVICE_STOP_SIGKILL:
831 /* Uh, wie sent a SIGKILL and it is still not gone?
832 * Must be something we cannot kill, so let's just be
833 * weirded out and continue */
835 log_warning("%s still around after SIGKILL. Ignoring.", name_id(n));
836 service_enter_stop_post(s, false);
839 case SERVICE_STOP_POST:
840 log_warning("%s stopping timed out (2). Terminating.", name_id(n));
841 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
844 case SERVICE_FINAL_SIGTERM:
845 log_warning("%s stopping timed out (2). Killing.", name_id(n));
846 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
849 case SERVICE_FINAL_SIGKILL:
850 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", name_id(n));
851 service_enter_dead(s, false, true);
854 case SERVICE_AUTO_RESTART:
855 log_debug("%s holdoff time over, scheduling restart.", name_id(n));
856 service_enter_restart(s);
860 assert_not_reached("Timeout at wrong time.");
864 const NameVTable service_vtable = {
865 .suffix = ".service",
867 .init = service_init,
868 .done = service_done,
870 .dump = service_dump,
872 .start = service_start,
873 .stop = service_stop,
874 .reload = service_reload,
876 .can_reload = service_can_reload,
878 .active_state = service_active_state,
880 .sigchld_event = service_sigchld_event,
881 .timer_event = service_timer_event,