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 /* 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 changed %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]))
475 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
479 service_set_state(s, SERVICE_STOP_POST);
481 if (!s->control_command)
482 service_enter_dead(s, true, true);
487 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
488 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
491 static void service_enter_signal(Service *s, ServiceState state, bool success) {
500 if (s->main_pid > 0 || s->control_pid > 0) {
503 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
506 if (s->main_pid > 0) {
507 if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
513 if (s->control_pid > 0) {
514 if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
524 service_set_state(s, state);
526 if (s->main_pid <= 0 && s->control_pid <= 0)
527 service_enter_dead(s, true, true);
532 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
536 service_set_state(s, state);
537 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
538 service_enter_stop_post(s, false);
540 service_enter_dead(s, false, true);
543 static void service_enter_stop(Service *s, bool success) {
550 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP]))
551 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
554 service_set_state(s, SERVICE_STOP);
556 if (!s->control_command)
557 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
562 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
563 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
566 static void service_enter_start_post(Service *s) {
570 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST]))
571 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
575 service_set_state(s, SERVICE_START_POST);
577 if (!s->control_command)
578 service_set_state(s, SERVICE_RUNNING);
583 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
584 service_enter_stop(s, false);
587 static void service_enter_start(Service *s) {
593 assert(s->exec_command[SERVICE_EXEC_START]);
594 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
596 if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
599 service_set_state(s, SERVICE_START);
601 if (s->type == SERVICE_SIMPLE) {
602 /* For simple services we immediately start
603 * the START_POST binaries. */
606 s->main_pid_known = true;
607 service_enter_start_post(s);
609 } else if (s->type == SERVICE_FORKING) {
611 /* For forking services we wait until the start
614 s->control_pid = pid;
615 s->control_command = s->exec_command[SERVICE_EXEC_START];
617 assert_not_reached("Unknown service type");
622 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
623 service_enter_stop(s, false);
626 static void service_enter_start_pre(Service *s) {
631 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]))
632 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
635 service_set_state(s, SERVICE_START_PRE);
637 if (!s->control_command)
638 service_enter_start(s);
643 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
644 service_enter_dead(s, false, true);
647 static void service_enter_restart(Service *s) {
651 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
654 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
655 service_enter_dead(s, true, false);
660 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
661 service_enter_dead(s, false, false);
664 static void service_enter_reload(Service *s) {
669 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]))
670 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
673 service_set_state(s, SERVICE_RELOAD);
675 if (!s->control_command)
676 service_set_state(s, SERVICE_RUNNING);
681 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
682 service_enter_stop(s, false);
685 static void service_run_next(Service *s, bool success) {
689 assert(s->control_command);
690 assert(s->control_command->command_next);
695 s->control_command = s->control_command->command_next;
697 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
703 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
705 if (s->state == SERVICE_STOP)
706 service_enter_stop_post(s, false);
707 else if (s->state == SERVICE_STOP_POST)
708 service_enter_dead(s, false, true);
710 service_enter_stop(s, false);
713 static int service_start(Unit *u) {
714 Service *s = SERVICE(u);
718 /* We cannot fulfill this request right now, try again later
720 if (s->state == SERVICE_STOP ||
721 s->state == SERVICE_STOP_SIGTERM ||
722 s->state == SERVICE_STOP_SIGKILL ||
723 s->state == SERVICE_STOP_POST ||
724 s->state == SERVICE_FINAL_SIGTERM ||
725 s->state == SERVICE_FINAL_SIGKILL)
729 if (s->state == SERVICE_START_PRE ||
730 s->state == SERVICE_START ||
731 s->state == SERVICE_START_POST)
734 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
737 s->main_pid_known = false;
739 service_enter_start_pre(s);
743 static int service_stop(Unit *u) {
744 Service *s = SERVICE(u);
748 if (s->state == SERVICE_START_PRE ||
749 s->state == SERVICE_START ||
750 s->state == SERVICE_START_POST ||
751 s->state == SERVICE_RELOAD)
754 if (s->state == SERVICE_AUTO_RESTART) {
755 service_set_state(s, SERVICE_DEAD);
759 assert(s->state == SERVICE_RUNNING);
761 service_enter_stop(s, true);
765 static int service_reload(Unit *u) {
766 Service *s = SERVICE(u);
770 assert(s->state == SERVICE_RUNNING);
772 service_enter_reload(s);
776 static bool service_can_reload(Unit *u) {
777 Service *s = SERVICE(u);
781 return !!s->exec_command[SERVICE_EXEC_RELOAD];
784 static UnitActiveState service_active_state(Unit *u) {
787 return state_translation_table[SERVICE(u)->state];
790 static int main_pid_good(Service *s) {
793 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
796 /* If we know the pid file, then lets just check if it is
798 if (s->main_pid_known)
799 return s->main_pid > 0;
801 /* We don't know the pid */
805 static bool control_pid_good(Service *s) {
808 return s->control_pid > 0;
811 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
812 Service *s = SERVICE(u);
818 success = code == CLD_EXITED && status == 0;
819 s->failure = s->failure || !success;
821 if (s->main_pid == pid) {
823 exec_status_fill(&s->main_exec_status, pid, code, status);
826 if (s->type == SERVICE_SIMPLE) {
827 assert(s->exec_command[SERVICE_EXEC_START]);
828 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
831 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
833 /* The service exited, so the service is officially
838 case SERVICE_START_POST:
841 /* Need to wait until the operation is
845 case SERVICE_RUNNING:
846 service_enter_stop(s, success);
849 case SERVICE_STOP_SIGTERM:
850 case SERVICE_STOP_SIGKILL:
852 if (!control_pid_good(s))
853 service_enter_stop_post(s, success);
855 /* If there is still a control process, wait for that first */
859 assert_not_reached("Uh, main process died at wrong time.");
862 } else if (s->control_pid == pid) {
863 assert(s->control_command);
865 exec_status_fill(&s->control_command->exec_status, pid, code, status);
868 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
870 /* If we are shutting things down anyway we
871 * don't care about failing commands. */
873 if (s->control_command->command_next &&
874 (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
876 /* There is another command to *
877 * execute, so let's do that. */
879 service_run_next(s, success);
882 /* No further commands for this step, so let's
883 * figure out what to do next */
885 log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
889 case SERVICE_START_PRE:
891 service_enter_start(s);
893 service_enter_stop(s, false);
897 assert(s->type == SERVICE_FORKING);
899 /* Let's try to load the pid
900 * file here if we can. We
901 * ignore the return value,
902 * since the PID file might
903 * actually be created by a
904 * START_POST script */
908 service_load_pid_file(s);
910 service_enter_start_post(s);
912 service_enter_stop(s, false);
916 case SERVICE_START_POST:
917 if (success && s->pid_file && !s->main_pid_known) {
920 /* Hmm, let's see if we can
921 * load the pid now after the
922 * start-post scripts got
925 if ((r = service_load_pid_file(s)) < 0)
926 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
933 if (main_pid_good(s) != 0)
934 service_set_state(s, SERVICE_RUNNING);
936 service_enter_stop(s, true);
938 service_enter_stop(s, false);
943 if (main_pid_good(s) > 0)
944 /* Still not dead and we know the PID? Let's go hunting. */
945 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
947 service_enter_stop_post(s, success);
950 case SERVICE_STOP_SIGTERM:
951 case SERVICE_STOP_SIGKILL:
952 if (main_pid_good(s) <= 0)
953 service_enter_stop_post(s, success);
955 /* If there is still a service
956 * process around, wait until
957 * that one quit, too */
960 case SERVICE_STOP_POST:
961 case SERVICE_FINAL_SIGTERM:
962 case SERVICE_FINAL_SIGKILL:
963 service_enter_dead(s, success, true);
967 assert_not_reached("Uh, control process died at wrong time.");
971 assert_not_reached("Got SIGCHLD for unkown PID");
974 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
975 Service *s = SERVICE(u);
978 assert(elapsed == 1);
980 assert(w == &s->timer_watch);
984 case SERVICE_START_PRE:
986 case SERVICE_START_POST:
988 log_warning("%s operation timed out. Stopping.", unit_id(u));
989 service_enter_stop(s, false);
993 log_warning("%s stopping timed out. Terminating.", unit_id(u));
994 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
997 case SERVICE_STOP_SIGTERM:
998 log_warning("%s stopping timed out. Killing.", unit_id(u));
999 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1002 case SERVICE_STOP_SIGKILL:
1003 /* Uh, wie sent a SIGKILL and it is still not gone?
1004 * Must be something we cannot kill, so let's just be
1005 * weirded out and continue */
1007 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1008 service_enter_stop_post(s, false);
1011 case SERVICE_STOP_POST:
1012 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1013 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1016 case SERVICE_FINAL_SIGTERM:
1017 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1018 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1021 case SERVICE_FINAL_SIGKILL:
1022 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1023 service_enter_dead(s, false, true);
1026 case SERVICE_AUTO_RESTART:
1027 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1028 service_enter_restart(s);
1032 assert_not_reached("Timeout at wrong time.");
1036 const UnitVTable service_vtable = {
1037 .suffix = ".service",
1039 .init = service_init,
1040 .done = service_done,
1042 .dump = service_dump,
1044 .start = service_start,
1045 .stop = service_stop,
1046 .reload = service_reload,
1048 .can_reload = service_can_reload,
1050 .active_state = service_active_state,
1052 .sigchld_event = service_sigchld_event,
1053 .timer_event = service_timer_event,