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 int service_get_sockets(Service *s, Set **_set) {
210 /* Collects all Socket objects that belong to this
211 * service. Note that a service might have multiple sockets
212 * via multiple names. */
214 if (!(set = set_new(NULL, NULL)))
217 SET_FOREACH(t, UNIT(s)->meta.names, i) {
221 /* Look for all socket objects that go by any of our
222 * units and collect their fds */
224 if (!(k = unit_name_change_suffix(t, ".socket"))) {
229 p = manager_get_unit(UNIT(s)->meta.manager, k);
234 if ((r = set_put(set, p)) < 0)
247 static int service_notify_sockets(Service *s) {
255 /* Notifies all our sockets when we die */
257 if ((r = service_get_sockets(s, &set)) < 0)
260 SET_FOREACH(socket, set, i)
261 socket_notify_service_dead(socket);
268 static void service_set_state(Service *s, ServiceState state) {
269 ServiceState old_state;
272 old_state = s->state;
275 if (state != SERVICE_START_PRE &&
276 state != SERVICE_START &&
277 state != SERVICE_START_POST &&
278 state != SERVICE_RELOAD &&
279 state != SERVICE_STOP &&
280 state != SERVICE_STOP_SIGTERM &&
281 state != SERVICE_STOP_SIGKILL &&
282 state != SERVICE_STOP_POST &&
283 state != SERVICE_FINAL_SIGTERM &&
284 state != SERVICE_FINAL_SIGKILL &&
285 state != SERVICE_AUTO_RESTART)
286 unit_unwatch_timer(UNIT(s), &s->timer_watch);
288 if (state != SERVICE_START_POST &&
289 state != SERVICE_RUNNING &&
290 state != SERVICE_RELOAD &&
291 state != SERVICE_STOP &&
292 state != SERVICE_STOP_SIGTERM &&
293 state != SERVICE_STOP_SIGKILL)
294 if (s->main_pid > 0) {
295 unit_unwatch_pid(UNIT(s), s->main_pid);
299 if (state != SERVICE_START_PRE &&
300 state != SERVICE_START &&
301 state != SERVICE_START_POST &&
302 state != SERVICE_RELOAD &&
303 state != SERVICE_STOP &&
304 state != SERVICE_STOP_SIGTERM &&
305 state != SERVICE_STOP_SIGKILL &&
306 state != SERVICE_STOP_POST &&
307 state != SERVICE_FINAL_SIGTERM &&
308 state != SERVICE_FINAL_SIGKILL)
309 if (s->control_pid > 0) {
310 unit_unwatch_pid(UNIT(s), s->control_pid);
314 if (state != SERVICE_START_PRE &&
315 state != SERVICE_START &&
316 state != SERVICE_START_POST &&
317 state != SERVICE_RELOAD &&
318 state != SERVICE_STOP &&
319 state != SERVICE_STOP_POST)
320 s->control_command = NULL;
322 if (state == SERVICE_DEAD ||
323 state == SERVICE_STOP ||
324 state == SERVICE_STOP_SIGTERM ||
325 state == SERVICE_STOP_SIGKILL ||
326 state == SERVICE_STOP_POST ||
327 state == SERVICE_FINAL_SIGTERM ||
328 state == SERVICE_FINAL_SIGKILL ||
329 state == SERVICE_MAINTAINANCE ||
330 state == SERVICE_AUTO_RESTART)
331 service_notify_sockets(s);
333 log_debug("%s changing %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
335 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
338 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
350 if ((r = service_get_sockets(s, &set)) < 0)
353 SET_FOREACH(socket, set, i) {
357 if ((r = socket_collect_fds(socket, &cfds, &cn_fds)) < 0)
369 if (!(t = new(int, rn_fds+cn_fds))) {
375 memcpy(t, rfds, rn_fds);
376 memcpy(t+rn_fds, cfds, cn_fds);
381 rn_fds = rn_fds+cn_fds;
399 static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds, pid_t *_pid) {
410 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
414 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
417 unit_unwatch_timer(UNIT(s), &s->timer_watch);
419 if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
422 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
423 /* FIXME: we need to do something here */
435 unit_unwatch_timer(UNIT(s), &s->timer_watch);
440 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
448 (s->restart == SERVICE_RESTART_ALWAYS ||
449 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
451 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
454 service_set_state(s, SERVICE_AUTO_RESTART);
456 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
461 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
462 service_enter_dead(s, false, false);
465 static void service_enter_signal(Service *s, ServiceState state, bool success);
467 static void service_enter_stop_post(Service *s, bool success) {
474 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
476 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
479 service_set_state(s, SERVICE_STOP_POST);
481 service_enter_dead(s, true, true);
486 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
487 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
490 static void service_enter_signal(Service *s, ServiceState state, bool success) {
499 if (s->main_pid > 0 || s->control_pid > 0) {
502 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
505 if (s->main_pid > 0) {
506 if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
512 if (s->control_pid > 0) {
513 if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
522 service_set_state(s, state);
524 service_enter_dead(s, true, true);
529 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
533 service_set_state(s, state);
534 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
535 service_enter_stop_post(s, false);
537 service_enter_dead(s, false, true);
540 static void service_enter_stop(Service *s, bool success) {
547 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
549 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
552 service_set_state(s, SERVICE_STOP);
554 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
559 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
560 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
563 static void service_enter_start_post(Service *s) {
567 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
569 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
572 service_set_state(s, SERVICE_START_POST);
574 service_set_state(s, SERVICE_RUNNING);
579 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
580 service_enter_stop(s, false);
583 static void service_enter_start(Service *s) {
589 assert(s->exec_command[SERVICE_EXEC_START]);
590 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
592 if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
595 if (s->type == SERVICE_SIMPLE) {
596 /* For simple services we immediately start
597 * the START_POST binaries. */
600 s->main_pid_known = true;
601 service_enter_start_post(s);
603 } else if (s->type == SERVICE_FORKING) {
605 /* For forking services we wait until the start
608 s->control_pid = pid;
609 s->control_command = s->exec_command[SERVICE_EXEC_START];
610 service_set_state(s, SERVICE_START);
612 assert_not_reached("Unknown service type");
617 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
618 service_enter_stop(s, false);
621 static void service_enter_start_pre(Service *s) {
626 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
628 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
631 service_set_state(s, SERVICE_START_PRE);
633 service_enter_start(s);
638 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
639 service_enter_dead(s, false, true);
642 static void service_enter_restart(Service *s) {
646 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
649 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
650 service_enter_dead(s, true, false);
655 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
656 service_enter_dead(s, false, false);
659 static void service_enter_reload(Service *s) {
664 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
666 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
669 service_set_state(s, SERVICE_RELOAD);
671 service_set_state(s, SERVICE_RUNNING);
676 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
677 service_enter_stop(s, false);
680 static void service_run_next(Service *s, bool success) {
684 assert(s->control_command);
685 assert(s->control_command->command_next);
690 s->control_command = s->control_command->command_next;
692 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
698 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
700 if (s->state == SERVICE_STOP)
701 service_enter_stop_post(s, false);
702 else if (s->state == SERVICE_STOP_POST)
703 service_enter_dead(s, false, true);
705 service_enter_stop(s, false);
708 static int service_start(Unit *u) {
709 Service *s = SERVICE(u);
713 /* We cannot fulfill this request right now, try again later
715 if (s->state == SERVICE_STOP ||
716 s->state == SERVICE_STOP_SIGTERM ||
717 s->state == SERVICE_STOP_SIGKILL ||
718 s->state == SERVICE_STOP_POST ||
719 s->state == SERVICE_FINAL_SIGTERM ||
720 s->state == SERVICE_FINAL_SIGKILL)
724 if (s->state == SERVICE_START_PRE ||
725 s->state == SERVICE_START ||
726 s->state == SERVICE_START_POST)
729 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
732 s->main_pid_known = false;
734 service_enter_start_pre(s);
738 static int service_stop(Unit *u) {
739 Service *s = SERVICE(u);
743 if (s->state == SERVICE_START_PRE ||
744 s->state == SERVICE_START ||
745 s->state == SERVICE_START_POST ||
746 s->state == SERVICE_RELOAD)
749 if (s->state == SERVICE_AUTO_RESTART) {
750 service_set_state(s, SERVICE_DEAD);
754 assert(s->state == SERVICE_RUNNING);
756 service_enter_stop(s, true);
760 static int service_reload(Unit *u) {
761 Service *s = SERVICE(u);
765 assert(s->state == SERVICE_RUNNING);
767 service_enter_reload(s);
771 static bool service_can_reload(Unit *u) {
772 Service *s = SERVICE(u);
776 return !!s->exec_command[SERVICE_EXEC_RELOAD];
779 static UnitActiveState service_active_state(Unit *u) {
782 return state_translation_table[SERVICE(u)->state];
785 static int main_pid_good(Service *s) {
788 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
791 /* If we know the pid file, then lets just check if it is
793 if (s->main_pid_known)
794 return s->main_pid > 0;
796 /* We don't know the pid */
800 static bool control_pid_good(Service *s) {
803 return s->control_pid > 0;
806 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
807 Service *s = SERVICE(u);
813 success = code == CLD_EXITED && status == 0;
814 s->failure = s->failure || !success;
816 if (s->main_pid == pid) {
818 exec_status_fill(&s->main_exec_status, pid, code, status);
821 if (s->type == SERVICE_SIMPLE) {
822 assert(s->exec_command[SERVICE_EXEC_START]);
823 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
826 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
828 /* The service exited, so the service is officially
833 case SERVICE_START_POST:
836 /* Need to wait until the operation is
840 case SERVICE_RUNNING:
841 service_enter_stop(s, success);
844 case SERVICE_STOP_SIGTERM:
845 case SERVICE_STOP_SIGKILL:
847 if (!control_pid_good(s))
848 service_enter_stop_post(s, success);
850 /* If there is still a control process, wait for that first */
854 assert_not_reached("Uh, main process died at wrong time.");
857 } else if (s->control_pid == pid) {
858 assert(s->control_command);
860 exec_status_fill(&s->control_command->exec_status, pid, code, status);
863 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
865 /* If we are shutting things down anyway we
866 * don't care about failing commands. */
868 if (s->control_command->command_next &&
869 (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
871 /* There is another command to *
872 * execute, so let's do that. */
874 service_run_next(s, success);
877 /* No further commands for this step, so let's
878 * figure out what to do next */
880 log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
884 case SERVICE_START_PRE:
886 service_enter_start(s);
888 service_enter_stop(s, false);
892 assert(s->type == SERVICE_FORKING);
894 /* Let's try to load the pid
895 * file here if we can. We
896 * ignore the return value,
897 * since the PID file might
898 * actually be created by a
899 * START_POST script */
903 service_load_pid_file(s);
905 service_enter_start_post(s);
907 service_enter_stop(s, false);
911 case SERVICE_START_POST:
912 if (success && s->pid_file && !s->main_pid_known) {
915 /* Hmm, let's see if we can
916 * load the pid now after the
917 * start-post scripts got
920 if ((r = service_load_pid_file(s)) < 0)
921 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
928 if (main_pid_good(s) != 0)
929 service_set_state(s, SERVICE_RUNNING);
931 service_enter_stop(s, true);
933 service_enter_stop(s, false);
938 if (main_pid_good(s) > 0)
939 /* Still not dead and we know the PID? Let's go hunting. */
940 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
942 service_enter_stop_post(s, success);
945 case SERVICE_STOP_SIGTERM:
946 case SERVICE_STOP_SIGKILL:
947 if (main_pid_good(s) <= 0)
948 service_enter_stop_post(s, success);
950 /* If there is still a service
951 * process around, wait until
952 * that one quit, too */
955 case SERVICE_STOP_POST:
956 case SERVICE_FINAL_SIGTERM:
957 case SERVICE_FINAL_SIGKILL:
958 service_enter_dead(s, success, true);
962 assert_not_reached("Uh, control process died at wrong time.");
966 assert_not_reached("Got SIGCHLD for unkown PID");
969 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
970 Service *s = SERVICE(u);
973 assert(elapsed == 1);
975 assert(w == &s->timer_watch);
979 case SERVICE_START_PRE:
981 case SERVICE_START_POST:
983 log_warning("%s operation timed out. Stopping.", unit_id(u));
984 service_enter_stop(s, false);
988 log_warning("%s stopping timed out. Terminating.", unit_id(u));
989 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
992 case SERVICE_STOP_SIGTERM:
993 log_warning("%s stopping timed out. Killing.", unit_id(u));
994 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
997 case SERVICE_STOP_SIGKILL:
998 /* Uh, wie sent a SIGKILL and it is still not gone?
999 * Must be something we cannot kill, so let's just be
1000 * weirded out and continue */
1002 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1003 service_enter_stop_post(s, false);
1006 case SERVICE_STOP_POST:
1007 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1008 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1011 case SERVICE_FINAL_SIGTERM:
1012 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1013 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1016 case SERVICE_FINAL_SIGKILL:
1017 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1018 service_enter_dead(s, false, true);
1021 case SERVICE_AUTO_RESTART:
1022 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1023 service_enter_restart(s);
1027 assert_not_reached("Timeout at wrong time.");
1031 const UnitVTable service_vtable = {
1032 .suffix = ".service",
1034 .init = service_init,
1035 .done = service_done,
1037 .dump = service_dump,
1039 .start = service_start,
1040 .stop = service_stop,
1041 .reload = service_reload,
1043 .can_reload = service_can_reload,
1045 .active_state = service_active_state,
1047 .sigchld_event = service_sigchld_event,
1048 .timer_event = service_timer_event,