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_notify_sockets(Service *s) {
207 SET_FOREACH(t, UNIT(s)->meta.names, i) {
211 /* Look for all socket objects that go by any of our
212 * units and collect their fds */
214 if (!(k = unit_name_change_suffix(t, ".socket")))
217 p = manager_get_unit(UNIT(s)->meta.manager, k);
223 socket_notify_service_dead(SOCKET(p));
229 static void service_set_state(Service *s, ServiceState state) {
230 ServiceState old_state;
233 old_state = s->state;
236 if (state != SERVICE_START_PRE &&
237 state != SERVICE_START &&
238 state != SERVICE_START_POST &&
239 state != SERVICE_RELOAD &&
240 state != SERVICE_STOP &&
241 state != SERVICE_STOP_SIGTERM &&
242 state != SERVICE_STOP_SIGKILL &&
243 state != SERVICE_STOP_POST &&
244 state != SERVICE_FINAL_SIGTERM &&
245 state != SERVICE_FINAL_SIGKILL &&
246 state != SERVICE_AUTO_RESTART)
247 unit_unwatch_timer(UNIT(s), &s->timer_watch);
249 if (state != SERVICE_START_POST &&
250 state != SERVICE_RUNNING &&
251 state != SERVICE_RELOAD &&
252 state != SERVICE_STOP &&
253 state != SERVICE_STOP_SIGTERM &&
254 state != SERVICE_STOP_SIGKILL)
255 if (s->main_pid > 0) {
256 unit_unwatch_pid(UNIT(s), s->main_pid);
260 if (state != SERVICE_START_PRE &&
261 state != SERVICE_START &&
262 state != SERVICE_START_POST &&
263 state != SERVICE_RELOAD &&
264 state != SERVICE_STOP &&
265 state != SERVICE_STOP_SIGTERM &&
266 state != SERVICE_STOP_SIGKILL &&
267 state != SERVICE_STOP_POST &&
268 state != SERVICE_FINAL_SIGTERM &&
269 state != SERVICE_FINAL_SIGKILL)
270 if (s->control_pid > 0) {
271 unit_unwatch_pid(UNIT(s), s->control_pid);
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_POST)
281 s->control_command = NULL;
283 if (state == SERVICE_DEAD ||
284 state == SERVICE_STOP ||
285 state == SERVICE_STOP_SIGTERM ||
286 state == SERVICE_STOP_SIGKILL ||
287 state == SERVICE_STOP_POST ||
288 state == SERVICE_FINAL_SIGTERM ||
289 state == SERVICE_FINAL_SIGKILL ||
290 state == SERVICE_MAINTAINANCE ||
291 state == SERVICE_AUTO_RESTART)
292 service_notify_sockets(s);
294 log_debug("%s changing %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
296 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
299 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
310 SET_FOREACH(t, UNIT(s)->meta.names, i) {
316 /* Look for all socket objects that go by any of our
317 * units and collect their fds */
319 if (!(k = unit_name_change_suffix(t, ".socket"))) {
324 p = manager_get_unit(UNIT(s)->meta.manager, k);
330 if ((r = socket_collect_fds(SOCKET(p), &cfds, &cn_fds)) < 0)
342 if (!(t = new(int, rn_fds+cn_fds))) {
348 memcpy(t, rfds, rn_fds);
349 memcpy(t+rn_fds, cfds, cn_fds);
354 rn_fds = rn_fds+cn_fds;
367 static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds, pid_t *_pid) {
378 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
382 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
385 unit_unwatch_timer(UNIT(s), &s->timer_watch);
387 if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
390 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
391 /* FIXME: we need to do something here */
403 unit_unwatch_timer(UNIT(s), &s->timer_watch);
408 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
416 (s->restart == SERVICE_RESTART_ALWAYS ||
417 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
419 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
422 service_set_state(s, SERVICE_AUTO_RESTART);
424 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
429 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
430 service_enter_dead(s, false, false);
433 static void service_enter_signal(Service *s, ServiceState state, bool success);
435 static void service_enter_stop_post(Service *s, bool success) {
442 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
444 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
447 service_set_state(s, SERVICE_STOP_POST);
449 service_enter_dead(s, true, true);
454 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
455 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
458 static void service_enter_signal(Service *s, ServiceState state, bool success) {
467 if (s->main_pid > 0 || s->control_pid > 0) {
470 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
473 if (s->main_pid > 0) {
474 if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
480 if (s->control_pid > 0) {
481 if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
490 service_set_state(s, state);
492 service_enter_dead(s, true, true);
497 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
501 service_set_state(s, state);
502 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
503 service_enter_stop_post(s, false);
505 service_enter_dead(s, false, true);
508 static void service_enter_stop(Service *s, bool success) {
515 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
517 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
520 service_set_state(s, SERVICE_STOP);
522 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
527 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
528 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
531 static void service_enter_start_post(Service *s) {
535 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
537 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
540 service_set_state(s, SERVICE_START_POST);
542 service_set_state(s, SERVICE_RUNNING);
547 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
548 service_enter_stop(s, false);
551 static void service_enter_start(Service *s) {
557 assert(s->exec_command[SERVICE_EXEC_START]);
558 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
560 if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
563 if (s->type == SERVICE_SIMPLE) {
564 /* For simple services we immediately start
565 * the START_POST binaries. */
568 s->main_pid_known = true;
569 service_enter_start_post(s);
571 } else if (s->type == SERVICE_FORKING) {
573 /* For forking services we wait until the start
576 s->control_pid = pid;
577 s->control_command = s->exec_command[SERVICE_EXEC_START];
578 service_set_state(s, SERVICE_START);
580 assert_not_reached("Unknown service type");
585 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
586 service_enter_stop(s, false);
589 static void service_enter_start_pre(Service *s) {
594 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
596 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
599 service_set_state(s, SERVICE_START_PRE);
601 service_enter_start(s);
606 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
607 service_enter_dead(s, false, true);
610 static void service_enter_restart(Service *s) {
614 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
617 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
618 service_enter_dead(s, true, false);
623 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
624 service_enter_dead(s, false, false);
627 static void service_enter_reload(Service *s) {
632 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
634 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
637 service_set_state(s, SERVICE_RELOAD);
639 service_set_state(s, SERVICE_RUNNING);
644 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
645 service_enter_stop(s, false);
648 static void service_run_next(Service *s, bool success) {
652 assert(s->control_command);
653 assert(s->control_command->command_next);
658 s->control_command = s->control_command->command_next;
660 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
666 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
668 if (s->state == SERVICE_STOP)
669 service_enter_stop_post(s, false);
670 else if (s->state == SERVICE_STOP_POST)
671 service_enter_dead(s, false, true);
673 service_enter_stop(s, false);
676 static int service_start(Unit *u) {
677 Service *s = SERVICE(u);
681 /* We cannot fulfill this request right now, try again later
683 if (s->state == SERVICE_STOP ||
684 s->state == SERVICE_STOP_SIGTERM ||
685 s->state == SERVICE_STOP_SIGKILL ||
686 s->state == SERVICE_STOP_POST ||
687 s->state == SERVICE_FINAL_SIGTERM ||
688 s->state == SERVICE_FINAL_SIGKILL)
692 if (s->state == SERVICE_START_PRE ||
693 s->state == SERVICE_START ||
694 s->state == SERVICE_START_POST)
697 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
700 s->main_pid_known = false;
702 service_enter_start_pre(s);
706 static int service_stop(Unit *u) {
707 Service *s = SERVICE(u);
711 if (s->state == SERVICE_START_PRE ||
712 s->state == SERVICE_START ||
713 s->state == SERVICE_START_POST ||
714 s->state == SERVICE_RELOAD)
717 if (s->state == SERVICE_AUTO_RESTART) {
718 service_set_state(s, SERVICE_DEAD);
722 assert(s->state == SERVICE_RUNNING);
724 service_enter_stop(s, true);
728 static int service_reload(Unit *u) {
729 Service *s = SERVICE(u);
733 assert(s->state == SERVICE_RUNNING);
735 service_enter_reload(s);
739 static bool service_can_reload(Unit *u) {
740 Service *s = SERVICE(u);
744 return !!s->exec_command[SERVICE_EXEC_RELOAD];
747 static UnitActiveState service_active_state(Unit *u) {
750 return state_translation_table[SERVICE(u)->state];
753 static int main_pid_good(Service *s) {
756 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
759 /* If we know the pid file, then lets just check if it is
761 if (s->main_pid_known)
762 return s->main_pid > 0;
764 /* We don't know the pid */
768 static bool control_pid_good(Service *s) {
771 return s->control_pid > 0;
774 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
775 Service *s = SERVICE(u);
781 success = code == CLD_EXITED && status == 0;
782 s->failure = s->failure || !success;
784 if (s->main_pid == pid) {
786 exec_status_fill(&s->main_exec_status, pid, code, status);
789 if (s->type == SERVICE_SIMPLE) {
790 assert(s->exec_command[SERVICE_EXEC_START]);
791 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
794 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
796 /* The service exited, so the service is officially
801 case SERVICE_START_POST:
804 /* Need to wait until the operation is
808 case SERVICE_RUNNING:
809 service_enter_stop(s, success);
812 case SERVICE_STOP_SIGTERM:
813 case SERVICE_STOP_SIGKILL:
815 if (!control_pid_good(s))
816 service_enter_stop_post(s, success);
818 /* If there is still a control process, wait for that first */
822 assert_not_reached("Uh, main process died at wrong time.");
825 } else if (s->control_pid == pid) {
826 assert(s->control_command);
828 exec_status_fill(&s->control_command->exec_status, pid, code, status);
831 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
833 /* If we are shutting things down anyway we
834 * don't care about failing commands. */
836 if (s->control_command->command_next &&
837 (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
839 /* There is another command to *
840 * execute, so let's do that. */
842 service_run_next(s, success);
845 /* No further commands for this step, so let's
846 * figure out what to do next */
848 log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
852 case SERVICE_START_PRE:
854 service_enter_start(s);
856 service_enter_stop(s, false);
860 assert(s->type == SERVICE_FORKING);
862 /* Let's try to load the pid
863 * file here if we can. We
864 * ignore the return value,
865 * since the PID file might
866 * actually be created by a
867 * START_POST script */
871 service_load_pid_file(s);
873 service_enter_start_post(s);
875 service_enter_stop(s, false);
879 case SERVICE_START_POST:
880 if (success && s->pid_file && !s->main_pid_known) {
883 /* Hmm, let's see if we can
884 * load the pid now after the
885 * start-post scripts got
888 if ((r = service_load_pid_file(s)) < 0)
889 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
896 if (main_pid_good(s) != 0)
897 service_set_state(s, SERVICE_RUNNING);
899 service_enter_stop(s, true);
901 service_enter_stop(s, false);
906 if (main_pid_good(s) > 0)
907 /* Still not dead and we know the PID? Let's go hunting. */
908 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
910 service_enter_stop_post(s, success);
913 case SERVICE_STOP_SIGTERM:
914 case SERVICE_STOP_SIGKILL:
915 if (main_pid_good(s) <= 0)
916 service_enter_stop_post(s, success);
918 /* If there is still a service
919 * process around, wait until
920 * that one quit, too */
923 case SERVICE_STOP_POST:
924 case SERVICE_FINAL_SIGTERM:
925 case SERVICE_FINAL_SIGKILL:
926 service_enter_dead(s, success, true);
930 assert_not_reached("Uh, control process died at wrong time.");
934 assert_not_reached("Got SIGCHLD for unkown PID");
937 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
938 Service *s = SERVICE(u);
941 assert(elapsed == 1);
943 assert(w == &s->timer_watch);
947 case SERVICE_START_PRE:
949 case SERVICE_START_POST:
951 log_warning("%s operation timed out. Stopping.", unit_id(u));
952 service_enter_stop(s, false);
956 log_warning("%s stopping timed out. Terminating.", unit_id(u));
957 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
960 case SERVICE_STOP_SIGTERM:
961 log_warning("%s stopping timed out. Killing.", unit_id(u));
962 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
965 case SERVICE_STOP_SIGKILL:
966 /* Uh, wie sent a SIGKILL and it is still not gone?
967 * Must be something we cannot kill, so let's just be
968 * weirded out and continue */
970 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
971 service_enter_stop_post(s, false);
974 case SERVICE_STOP_POST:
975 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
976 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
979 case SERVICE_FINAL_SIGTERM:
980 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
981 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
984 case SERVICE_FINAL_SIGKILL:
985 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
986 service_enter_dead(s, false, true);
989 case SERVICE_AUTO_RESTART:
990 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
991 service_enter_restart(s);
995 assert_not_reached("Timeout at wrong time.");
999 const UnitVTable service_vtable = {
1000 .suffix = ".service",
1002 .init = service_init,
1003 .done = service_done,
1005 .dump = service_dump,
1007 .start = service_start,
1008 .stop = service_stop,
1009 .reload = service_reload,
1011 .can_reload = service_can_reload,
1013 .active_state = service_active_state,
1015 .sigchld_event = service_sigchld_event,
1016 .timer_event = service_timer_event,