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] = "start-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 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
105 /* Load a .service file */
106 if ((r = unit_load_fragment(u)) < 0) {
111 /* Load a classic init script as a fallback, if we couldn*t find anything */
113 if ((r = service_load_sysv(s)) <= 0) {
115 return r < 0 ? r : -ENOENT;
118 /* Load dropin directory data */
119 if ((r = unit_load_dropin(u)) < 0) {
127 static void service_dump(Unit *u, FILE *f, const char *prefix) {
129 static const char* const command_table[_SERVICE_EXEC_MAX] = {
130 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
131 [SERVICE_EXEC_START] = "ExecStart",
132 [SERVICE_EXEC_START_POST] = "ExecStartPost",
133 [SERVICE_EXEC_RELOAD] = "ExecReload",
134 [SERVICE_EXEC_STOP] = "ExecStop",
135 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
138 ServiceExecCommand c;
139 Service *s = SERVICE(u);
144 prefix2 = strappend(prefix, "\t");
149 "%sService State: %s\n",
150 prefix, state_string_table[s->state]);
155 prefix, s->pid_file);
158 exec_context_dump(&s->exec_context, f, prefix);
160 for (c = 0; c < _SERVICE_EXEC_MAX; c++) {
162 if (!s->exec_command[c])
165 fprintf(f, "%s→ %s:\n",
166 prefix, command_table[c]);
168 exec_command_dump_list(s->exec_command[c], f, prefix2);
174 static int service_load_pid_file(Service *s) {
181 if (s->main_pid_known)
187 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
190 if ((r = safe_atolu(k, &p)) < 0) {
195 if ((unsigned long) (pid_t) p != p)
199 s->main_pid_known = true;
204 static int service_get_sockets(Service *s, Set **_set) {
213 /* Collects all Socket objects that belong to this
214 * service. Note that a service might have multiple sockets
215 * via multiple names. */
217 if (!(set = set_new(NULL, NULL)))
220 SET_FOREACH(t, UNIT(s)->meta.names, i) {
224 /* Look for all socket objects that go by any of our
225 * units and collect their fds */
227 if (!(k = unit_name_change_suffix(t, ".socket"))) {
232 p = manager_get_unit(UNIT(s)->meta.manager, k);
237 if ((r = set_put(set, p)) < 0)
250 static int service_notify_sockets(Service *s) {
258 /* Notifies all our sockets when we die */
260 if ((r = service_get_sockets(s, &set)) < 0)
263 SET_FOREACH(socket, set, i)
264 socket_notify_service_dead(socket);
271 static void service_set_state(Service *s, ServiceState state) {
272 ServiceState old_state;
275 old_state = s->state;
278 if (state != SERVICE_START_PRE &&
279 state != SERVICE_START &&
280 state != SERVICE_START_POST &&
281 state != SERVICE_RELOAD &&
282 state != SERVICE_STOP &&
283 state != SERVICE_STOP_SIGTERM &&
284 state != SERVICE_STOP_SIGKILL &&
285 state != SERVICE_STOP_POST &&
286 state != SERVICE_FINAL_SIGTERM &&
287 state != SERVICE_FINAL_SIGKILL &&
288 state != SERVICE_AUTO_RESTART)
289 unit_unwatch_timer(UNIT(s), &s->timer_watch);
291 if (state != SERVICE_START_POST &&
292 state != SERVICE_RUNNING &&
293 state != SERVICE_RELOAD &&
294 state != SERVICE_STOP &&
295 state != SERVICE_STOP_SIGTERM &&
296 state != SERVICE_STOP_SIGKILL)
297 if (s->main_pid > 0) {
298 unit_unwatch_pid(UNIT(s), s->main_pid);
302 if (state != SERVICE_START_PRE &&
303 state != SERVICE_START &&
304 state != SERVICE_START_POST &&
305 state != SERVICE_RELOAD &&
306 state != SERVICE_STOP &&
307 state != SERVICE_STOP_SIGTERM &&
308 state != SERVICE_STOP_SIGKILL &&
309 state != SERVICE_STOP_POST &&
310 state != SERVICE_FINAL_SIGTERM &&
311 state != SERVICE_FINAL_SIGKILL)
312 if (s->control_pid > 0) {
313 unit_unwatch_pid(UNIT(s), s->control_pid);
317 if (state != SERVICE_START_PRE &&
318 state != SERVICE_START &&
319 state != SERVICE_START_POST &&
320 state != SERVICE_RELOAD &&
321 state != SERVICE_STOP &&
322 state != SERVICE_STOP_POST)
323 s->control_command = NULL;
325 if (state == SERVICE_DEAD ||
326 state == SERVICE_STOP ||
327 state == SERVICE_STOP_SIGTERM ||
328 state == SERVICE_STOP_SIGKILL ||
329 state == SERVICE_STOP_POST ||
330 state == SERVICE_FINAL_SIGTERM ||
331 state == SERVICE_FINAL_SIGKILL ||
332 state == SERVICE_MAINTAINANCE ||
333 state == SERVICE_AUTO_RESTART)
334 service_notify_sockets(s);
336 log_debug("%s changed %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
338 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
341 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
353 if ((r = service_get_sockets(s, &set)) < 0)
356 SET_FOREACH(socket, set, i) {
360 if ((r = socket_collect_fds(socket, &cfds, &cn_fds)) < 0)
372 if (!(t = new(int, rn_fds+cn_fds))) {
378 memcpy(t, rfds, rn_fds);
379 memcpy(t+rn_fds, cfds, cn_fds);
384 rn_fds = rn_fds+cn_fds;
402 static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds, pid_t *_pid) {
413 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
417 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
420 unit_unwatch_timer(UNIT(s), &s->timer_watch);
422 if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
425 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
426 /* FIXME: we need to do something here */
438 unit_unwatch_timer(UNIT(s), &s->timer_watch);
443 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
451 (s->restart == SERVICE_RESTART_ALWAYS ||
452 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
454 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
457 service_set_state(s, SERVICE_AUTO_RESTART);
459 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
464 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
465 service_enter_dead(s, false, false);
468 static void service_enter_signal(Service *s, ServiceState state, bool success);
470 static void service_enter_stop_post(Service *s, bool success) {
477 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]))
478 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
482 service_set_state(s, SERVICE_STOP_POST);
484 if (!s->control_command)
485 service_enter_dead(s, true, true);
490 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
491 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
494 static void service_enter_signal(Service *s, ServiceState state, bool success) {
503 if (s->main_pid > 0 || s->control_pid > 0) {
506 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
509 if (s->main_pid > 0) {
510 if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
516 if (s->control_pid > 0) {
517 if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
527 service_set_state(s, state);
529 if (s->main_pid <= 0 && s->control_pid <= 0)
530 service_enter_dead(s, true, true);
535 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
539 service_set_state(s, state);
540 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
541 service_enter_stop_post(s, false);
543 service_enter_dead(s, false, true);
546 static void service_enter_stop(Service *s, bool success) {
553 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP]))
554 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
557 service_set_state(s, SERVICE_STOP);
559 if (!s->control_command)
560 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
565 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
566 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
569 static void service_enter_start_post(Service *s) {
573 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST]))
574 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
578 service_set_state(s, SERVICE_START_POST);
580 if (!s->control_command)
581 service_set_state(s, SERVICE_RUNNING);
586 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
587 service_enter_stop(s, false);
590 static void service_enter_start(Service *s) {
596 assert(s->exec_command[SERVICE_EXEC_START]);
597 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
599 if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
602 service_set_state(s, SERVICE_START);
604 if (s->type == SERVICE_SIMPLE) {
605 /* For simple services we immediately start
606 * the START_POST binaries. */
609 s->main_pid_known = true;
610 service_enter_start_post(s);
612 } else if (s->type == SERVICE_FORKING) {
614 /* For forking services we wait until the start
617 s->control_pid = pid;
618 s->control_command = s->exec_command[SERVICE_EXEC_START];
620 assert_not_reached("Unknown service type");
625 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
626 service_enter_stop(s, false);
629 static void service_enter_start_pre(Service *s) {
634 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]))
635 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
638 service_set_state(s, SERVICE_START_PRE);
640 if (!s->control_command)
641 service_enter_start(s);
646 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
647 service_enter_dead(s, false, true);
650 static void service_enter_restart(Service *s) {
654 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
657 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
658 service_enter_dead(s, true, false);
663 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
664 service_enter_dead(s, false, false);
667 static void service_enter_reload(Service *s) {
672 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]))
673 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
676 service_set_state(s, SERVICE_RELOAD);
678 if (!s->control_command)
679 service_set_state(s, SERVICE_RUNNING);
684 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
685 service_enter_stop(s, false);
688 static void service_run_next(Service *s, bool success) {
692 assert(s->control_command);
693 assert(s->control_command->command_next);
698 s->control_command = s->control_command->command_next;
700 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
706 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
708 if (s->state == SERVICE_STOP)
709 service_enter_stop_post(s, false);
710 else if (s->state == SERVICE_STOP_POST)
711 service_enter_dead(s, false, true);
713 service_enter_stop(s, false);
716 static int service_start(Unit *u) {
717 Service *s = SERVICE(u);
721 /* We cannot fulfill this request right now, try again later
723 if (s->state == SERVICE_STOP ||
724 s->state == SERVICE_STOP_SIGTERM ||
725 s->state == SERVICE_STOP_SIGKILL ||
726 s->state == SERVICE_STOP_POST ||
727 s->state == SERVICE_FINAL_SIGTERM ||
728 s->state == SERVICE_FINAL_SIGKILL)
732 if (s->state == SERVICE_START_PRE ||
733 s->state == SERVICE_START ||
734 s->state == SERVICE_START_POST)
737 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
739 /* Make sure we don't enter a busy loop of some kind. */
740 if (!ratelimit_test(&s->ratelimit)) {
741 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u));
746 s->main_pid_known = false;
748 service_enter_start_pre(s);
752 static int service_stop(Unit *u) {
753 Service *s = SERVICE(u);
757 if (s->state == SERVICE_START_PRE ||
758 s->state == SERVICE_START ||
759 s->state == SERVICE_START_POST ||
760 s->state == SERVICE_RELOAD)
763 if (s->state == SERVICE_AUTO_RESTART) {
764 service_set_state(s, SERVICE_DEAD);
768 assert(s->state == SERVICE_RUNNING);
770 service_enter_stop(s, true);
774 static int service_reload(Unit *u) {
775 Service *s = SERVICE(u);
779 assert(s->state == SERVICE_RUNNING);
781 service_enter_reload(s);
785 static bool service_can_reload(Unit *u) {
786 Service *s = SERVICE(u);
790 return !!s->exec_command[SERVICE_EXEC_RELOAD];
793 static UnitActiveState service_active_state(Unit *u) {
796 return state_translation_table[SERVICE(u)->state];
799 static int main_pid_good(Service *s) {
802 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
805 /* If we know the pid file, then lets just check if it is
807 if (s->main_pid_known)
808 return s->main_pid > 0;
810 /* We don't know the pid */
814 static bool control_pid_good(Service *s) {
817 return s->control_pid > 0;
820 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
821 Service *s = SERVICE(u);
827 success = code == CLD_EXITED && status == 0;
828 s->failure = s->failure || !success;
830 if (s->main_pid == pid) {
832 exec_status_fill(&s->main_exec_status, pid, code, status);
835 if (s->type == SERVICE_SIMPLE) {
836 assert(s->exec_command[SERVICE_EXEC_START]);
837 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
840 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
842 /* The service exited, so the service is officially
847 case SERVICE_START_POST:
850 /* Need to wait until the operation is
854 case SERVICE_RUNNING:
855 service_enter_stop(s, success);
858 case SERVICE_STOP_SIGTERM:
859 case SERVICE_STOP_SIGKILL:
861 if (!control_pid_good(s))
862 service_enter_stop_post(s, success);
864 /* If there is still a control process, wait for that first */
868 assert_not_reached("Uh, main process died at wrong time.");
871 } else if (s->control_pid == pid) {
872 assert(s->control_command);
874 exec_status_fill(&s->control_command->exec_status, pid, code, status);
877 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
879 /* If we are shutting things down anyway we
880 * don't care about failing commands. */
882 if (s->control_command->command_next &&
883 (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
885 /* There is another command to *
886 * execute, so let's do that. */
888 service_run_next(s, success);
891 /* No further commands for this step, so let's
892 * figure out what to do next */
894 log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
898 case SERVICE_START_PRE:
900 service_enter_start(s);
902 service_enter_stop(s, false);
906 assert(s->type == SERVICE_FORKING);
908 /* Let's try to load the pid
909 * file here if we can. We
910 * ignore the return value,
911 * since the PID file might
912 * actually be created by a
913 * START_POST script */
917 service_load_pid_file(s);
919 service_enter_start_post(s);
921 service_enter_stop(s, false);
925 case SERVICE_START_POST:
926 if (success && s->pid_file && !s->main_pid_known) {
929 /* Hmm, let's see if we can
930 * load the pid now after the
931 * start-post scripts got
934 if ((r = service_load_pid_file(s)) < 0)
935 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
942 if (main_pid_good(s) != 0)
943 service_set_state(s, SERVICE_RUNNING);
945 service_enter_stop(s, true);
947 service_enter_stop(s, false);
952 if (main_pid_good(s) > 0)
953 /* Still not dead and we know the PID? Let's go hunting. */
954 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
956 service_enter_stop_post(s, success);
959 case SERVICE_STOP_SIGTERM:
960 case SERVICE_STOP_SIGKILL:
961 if (main_pid_good(s) <= 0)
962 service_enter_stop_post(s, success);
964 /* If there is still a service
965 * process around, wait until
966 * that one quit, too */
969 case SERVICE_STOP_POST:
970 case SERVICE_FINAL_SIGTERM:
971 case SERVICE_FINAL_SIGKILL:
972 service_enter_dead(s, success, true);
976 assert_not_reached("Uh, control process died at wrong time.");
980 assert_not_reached("Got SIGCHLD for unkown PID");
983 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
984 Service *s = SERVICE(u);
987 assert(elapsed == 1);
989 assert(w == &s->timer_watch);
993 case SERVICE_START_PRE:
995 case SERVICE_START_POST:
997 log_warning("%s operation timed out. Stopping.", unit_id(u));
998 service_enter_stop(s, false);
1002 log_warning("%s stopping timed out. Terminating.", unit_id(u));
1003 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1006 case SERVICE_STOP_SIGTERM:
1007 log_warning("%s stopping timed out. Killing.", unit_id(u));
1008 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1011 case SERVICE_STOP_SIGKILL:
1012 /* Uh, wie sent a SIGKILL and it is still not gone?
1013 * Must be something we cannot kill, so let's just be
1014 * weirded out and continue */
1016 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1017 service_enter_stop_post(s, false);
1020 case SERVICE_STOP_POST:
1021 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1022 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1025 case SERVICE_FINAL_SIGTERM:
1026 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1027 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1030 case SERVICE_FINAL_SIGKILL:
1031 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1032 service_enter_dead(s, false, true);
1035 case SERVICE_AUTO_RESTART:
1036 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1037 service_enter_restart(s);
1041 assert_not_reached("Timeout at wrong time.");
1045 const UnitVTable service_vtable = {
1046 .suffix = ".service",
1048 .init = service_init,
1049 .done = service_done,
1051 .dump = service_dump,
1053 .start = service_start,
1054 .stop = service_stop,
1055 .reload = service_reload,
1057 .can_reload = service_can_reload,
1059 .active_state = service_active_state,
1061 .sigchld_event = service_sigchld_event,
1062 .timer_event = service_timer_event,