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);
288 if ((r = socket_collect_fds(SOCKET(p), &cfds, &cn_fds)) < 0)
300 if (!(t = new(int, rn_fds+cn_fds))) {
306 memcpy(t, rfds, rn_fds);
307 memcpy(t+rn_fds, cfds, cn_fds);
312 rn_fds = rn_fds+cn_fds;
325 static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds, pid_t *_pid) {
336 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
340 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
343 unit_unwatch_timer(UNIT(s), &s->timer_watch);
345 if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
348 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
349 /* FIXME: we need to do something here */
361 unit_unwatch_timer(UNIT(s), &s->timer_watch);
366 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
374 (s->restart == SERVICE_RESTART_ALWAYS ||
375 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
377 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
380 service_set_state(s, SERVICE_AUTO_RESTART);
382 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
387 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
388 service_enter_dead(s, false, false);
391 static void service_enter_signal(Service *s, ServiceState state, bool success);
393 static void service_enter_stop_post(Service *s, bool success) {
400 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
402 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
405 service_set_state(s, SERVICE_STOP_POST);
407 service_enter_dead(s, true, true);
412 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
413 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
416 static void service_enter_signal(Service *s, ServiceState state, bool success) {
425 if (s->main_pid > 0 || s->control_pid > 0) {
428 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
431 if (s->main_pid > 0) {
432 if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
438 if (s->control_pid > 0) {
439 if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
448 service_set_state(s, state);
450 service_enter_dead(s, true, true);
455 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
459 service_set_state(s, state);
460 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
461 service_enter_stop_post(s, false);
463 service_enter_dead(s, false, true);
466 static void service_enter_stop(Service *s, bool success) {
473 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
475 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
478 service_set_state(s, SERVICE_STOP);
480 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
485 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
486 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
489 static void service_enter_start_post(Service *s) {
493 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
495 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
498 service_set_state(s, SERVICE_START_POST);
500 service_set_state(s, SERVICE_RUNNING);
505 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
506 service_enter_stop(s, false);
509 static void service_enter_start(Service *s) {
515 assert(s->exec_command[SERVICE_EXEC_START]);
516 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
518 if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
521 if (s->type == SERVICE_SIMPLE) {
522 /* For simple services we immediately start
523 * the START_POST binaries. */
526 s->main_pid_known = true;
527 service_enter_start_post(s);
529 } else if (s->type == SERVICE_FORKING) {
531 /* For forking services we wait until the start
534 s->control_pid = pid;
535 s->control_command = s->exec_command[SERVICE_EXEC_START];
536 service_set_state(s, SERVICE_START);
538 assert_not_reached("Unknown service type");
543 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
544 service_enter_stop(s, false);
547 static void service_enter_start_pre(Service *s) {
552 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
554 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
557 service_set_state(s, SERVICE_START_PRE);
559 service_enter_start(s);
564 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
565 service_enter_dead(s, false, true);
568 static void service_enter_restart(Service *s) {
572 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
575 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
576 service_enter_dead(s, true, false);
581 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
582 service_enter_dead(s, false, false);
585 static void service_enter_reload(Service *s) {
590 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
592 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
595 service_set_state(s, SERVICE_RELOAD);
597 service_set_state(s, SERVICE_RUNNING);
602 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
603 service_enter_stop(s, false);
606 static void service_run_next(Service *s, bool success) {
610 assert(s->control_command);
611 assert(s->control_command->command_next);
616 s->control_command = s->control_command->command_next;
618 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
624 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
626 if (s->state == SERVICE_STOP)
627 service_enter_stop_post(s, false);
628 else if (s->state == SERVICE_STOP_POST)
629 service_enter_dead(s, false, true);
631 service_enter_stop(s, false);
634 static int service_start(Unit *u) {
635 Service *s = SERVICE(u);
639 /* We cannot fulfill this request right now, try again later
641 if (s->state == SERVICE_STOP ||
642 s->state == SERVICE_STOP_SIGTERM ||
643 s->state == SERVICE_STOP_SIGKILL ||
644 s->state == SERVICE_STOP_POST ||
645 s->state == SERVICE_FINAL_SIGTERM ||
646 s->state == SERVICE_FINAL_SIGKILL)
650 if (s->state == SERVICE_START_PRE ||
651 s->state == SERVICE_START ||
652 s->state == SERVICE_START_POST)
655 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
658 s->main_pid_known = false;
660 service_enter_start_pre(s);
664 static int service_stop(Unit *u) {
665 Service *s = SERVICE(u);
669 if (s->state == SERVICE_START_PRE ||
670 s->state == SERVICE_START ||
671 s->state == SERVICE_START_POST ||
672 s->state == SERVICE_RELOAD)
675 if (s->state == SERVICE_AUTO_RESTART) {
676 service_set_state(s, SERVICE_DEAD);
680 assert(s->state == SERVICE_RUNNING);
682 service_enter_stop(s, true);
686 static int service_reload(Unit *u) {
687 Service *s = SERVICE(u);
691 assert(s->state == SERVICE_RUNNING);
693 service_enter_reload(s);
697 static bool service_can_reload(Unit *u) {
698 Service *s = SERVICE(u);
702 return !!s->exec_command[SERVICE_EXEC_RELOAD];
705 static UnitActiveState service_active_state(Unit *u) {
708 return state_translation_table[SERVICE(u)->state];
711 static int main_pid_good(Service *s) {
714 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
717 /* If we know the pid file, then lets just check if it is
719 if (s->main_pid_known)
720 return s->main_pid > 0;
722 /* We don't know the pid */
726 static bool control_pid_good(Service *s) {
729 return s->control_pid > 0;
732 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
733 Service *s = SERVICE(u);
739 success = code == CLD_EXITED || status == 0;
740 s->failure = s->failure || !success;
742 if (s->main_pid == pid) {
744 exec_status_fill(&s->main_exec_status, pid, code, status);
747 if (s->type == SERVICE_SIMPLE) {
748 assert(s->exec_command[SERVICE_EXEC_START]);
749 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
752 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
754 /* The service exited, so the service is officially
759 case SERVICE_START_POST:
762 /* Need to wait until the operation is
766 case SERVICE_RUNNING:
767 service_enter_stop(s, success);
770 case SERVICE_STOP_SIGTERM:
771 case SERVICE_STOP_SIGKILL:
773 if (!control_pid_good(s))
774 service_enter_stop_post(s, success);
776 /* If there is still a control process, wait for that first */
780 assert_not_reached("Uh, main process died at wrong time.");
783 } else if (s->control_pid == pid) {
784 assert(s->control_command);
786 exec_status_fill(&s->control_command->exec_status, pid, code, status);
789 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
791 /* If we are shutting things down anyway we
792 * don't care about failing commands. */
794 if (s->control_command->command_next &&
795 (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
797 /* There is another command to *
798 * execute, so let's do that. */
800 service_run_next(s, success);
803 /* No further commands for this step, so let's
804 * figure out what to do next */
808 case SERVICE_START_PRE:
810 service_enter_start(s);
812 service_enter_stop(s, false);
816 assert(s->type == SERVICE_FORKING);
818 /* Let's try to load the pid
819 * file here if we can. We
820 * ignore the return value,
821 * since the PID file might
822 * actually be created by a
823 * START_POST script */
827 service_load_pid_file(s);
829 service_enter_start_post(s);
831 service_enter_stop(s, false);
835 case SERVICE_START_POST:
836 if (success && s->pid_file && !s->main_pid_known) {
839 /* Hmm, let's see if we can
840 * load the pid now after the
841 * start-post scripts got
844 if ((r = service_load_pid_file(s)) < 0)
845 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
852 if (main_pid_good(s) != 0)
853 service_set_state(s, SERVICE_RUNNING);
855 service_enter_stop(s, true);
857 service_enter_stop(s, false);
862 if (main_pid_good(s) > 0)
863 /* Still not dead and we know the PID? Let's go hunting. */
864 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
866 service_enter_stop_post(s, success);
869 case SERVICE_STOP_SIGTERM:
870 case SERVICE_STOP_SIGKILL:
871 if (main_pid_good(s) <= 0)
872 service_enter_stop_post(s, success);
874 /* If there is still a service
875 * process around, wait until
876 * that one quit, too */
879 case SERVICE_STOP_POST:
880 case SERVICE_FINAL_SIGTERM:
881 case SERVICE_FINAL_SIGKILL:
882 service_enter_dead(s, success, true);
886 assert_not_reached("Uh, control process died at wrong time.");
890 assert_not_reached("Got SIGCHLD for unkown PID");
893 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
894 Service *s = SERVICE(u);
897 assert(elapsed == 1);
899 assert(w == &s->timer_watch);
903 case SERVICE_START_PRE:
905 case SERVICE_START_POST:
907 log_warning("%s operation timed out. Stopping.", unit_id(u));
908 service_enter_stop(s, false);
912 log_warning("%s stopping timed out. Terminating.", unit_id(u));
913 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
916 case SERVICE_STOP_SIGTERM:
917 log_warning("%s stopping timed out. Killing.", unit_id(u));
918 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
921 case SERVICE_STOP_SIGKILL:
922 /* Uh, wie sent a SIGKILL and it is still not gone?
923 * Must be something we cannot kill, so let's just be
924 * weirded out and continue */
926 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
927 service_enter_stop_post(s, false);
930 case SERVICE_STOP_POST:
931 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
932 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
935 case SERVICE_FINAL_SIGTERM:
936 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
937 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
940 case SERVICE_FINAL_SIGKILL:
941 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
942 service_enter_dead(s, false, true);
945 case SERVICE_AUTO_RESTART:
946 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
947 service_enter_restart(s);
951 assert_not_reached("Timeout at wrong time.");
955 const UnitVTable service_vtable = {
956 .suffix = ".service",
958 .init = service_init,
959 .done = service_done,
961 .dump = service_dump,
963 .start = service_start,
964 .stop = service_stop,
965 .reload = service_reload,
967 .can_reload = service_can_reload,
969 .active_state = service_active_state,
971 .sigchld_event = service_sigchld_event,
972 .timer_event = service_timer_event,