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 if ((r = unit_load_fragment(u)) < 0) {
109 /* Load a classic init script as a fallback, if we couldn*t find anything */
111 if ((r = service_load_sysv(s)) <= 0) {
113 return r < 0 ? r : -ENOENT;
116 /* Load dropin directory data */
117 if ((r = unit_load_dropin(u)) < 0) {
125 static void service_dump(Unit *u, FILE *f, const char *prefix) {
127 static const char* const command_table[_SERVICE_EXEC_MAX] = {
128 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
129 [SERVICE_EXEC_START] = "ExecStart",
130 [SERVICE_EXEC_START_POST] = "ExecStartPost",
131 [SERVICE_EXEC_RELOAD] = "ExecReload",
132 [SERVICE_EXEC_STOP] = "ExecStop",
133 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
136 ServiceExecCommand c;
137 Service *s = SERVICE(u);
142 prefix2 = strappend(prefix, "\t");
147 "%sService State: %s\n",
148 prefix, state_string_table[s->state]);
153 prefix, s->pid_file);
156 exec_context_dump(&s->exec_context, f, prefix);
158 for (c = 0; c < _SERVICE_EXEC_MAX; c++) {
160 if (!s->exec_command[c])
163 fprintf(f, "%s→ %s:\n",
164 prefix, command_table[c]);
166 exec_command_dump_list(s->exec_command[c], f, prefix2);
172 static int service_load_pid_file(Service *s) {
179 if (s->main_pid_known)
185 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
188 if ((r = safe_atolu(k, &p)) < 0) {
193 if ((unsigned long) (pid_t) p != p)
197 s->main_pid_known = true;
202 static int service_get_sockets(Service *s, Set **_set) {
211 /* Collects all Socket objects that belong to this
212 * service. Note that a service might have multiple sockets
213 * via multiple names. */
215 if (!(set = set_new(NULL, NULL)))
218 SET_FOREACH(t, UNIT(s)->meta.names, i) {
222 /* Look for all socket objects that go by any of our
223 * units and collect their fds */
225 if (!(k = unit_name_change_suffix(t, ".socket"))) {
230 p = manager_get_unit(UNIT(s)->meta.manager, k);
235 if ((r = set_put(set, p)) < 0)
248 static int service_notify_sockets(Service *s) {
256 /* Notifies all our sockets when we die */
258 if ((r = service_get_sockets(s, &set)) < 0)
261 SET_FOREACH(socket, set, i)
262 socket_notify_service_dead(socket);
269 static void service_set_state(Service *s, ServiceState state) {
270 ServiceState old_state;
273 old_state = s->state;
276 if (state != SERVICE_START_PRE &&
277 state != SERVICE_START &&
278 state != SERVICE_START_POST &&
279 state != SERVICE_RELOAD &&
280 state != SERVICE_STOP &&
281 state != SERVICE_STOP_SIGTERM &&
282 state != SERVICE_STOP_SIGKILL &&
283 state != SERVICE_STOP_POST &&
284 state != SERVICE_FINAL_SIGTERM &&
285 state != SERVICE_FINAL_SIGKILL &&
286 state != SERVICE_AUTO_RESTART)
287 unit_unwatch_timer(UNIT(s), &s->timer_watch);
289 if (state != SERVICE_START_POST &&
290 state != SERVICE_RUNNING &&
291 state != SERVICE_RELOAD &&
292 state != SERVICE_STOP &&
293 state != SERVICE_STOP_SIGTERM &&
294 state != SERVICE_STOP_SIGKILL)
295 if (s->main_pid > 0) {
296 unit_unwatch_pid(UNIT(s), s->main_pid);
300 if (state != SERVICE_START_PRE &&
301 state != SERVICE_START &&
302 state != SERVICE_START_POST &&
303 state != SERVICE_RELOAD &&
304 state != SERVICE_STOP &&
305 state != SERVICE_STOP_SIGTERM &&
306 state != SERVICE_STOP_SIGKILL &&
307 state != SERVICE_STOP_POST &&
308 state != SERVICE_FINAL_SIGTERM &&
309 state != SERVICE_FINAL_SIGKILL)
310 if (s->control_pid > 0) {
311 unit_unwatch_pid(UNIT(s), s->control_pid);
315 if (state != SERVICE_START_PRE &&
316 state != SERVICE_START &&
317 state != SERVICE_START_POST &&
318 state != SERVICE_RELOAD &&
319 state != SERVICE_STOP &&
320 state != SERVICE_STOP_POST)
321 s->control_command = NULL;
323 if (state == SERVICE_DEAD ||
324 state == SERVICE_STOP ||
325 state == SERVICE_STOP_SIGTERM ||
326 state == SERVICE_STOP_SIGKILL ||
327 state == SERVICE_STOP_POST ||
328 state == SERVICE_FINAL_SIGTERM ||
329 state == SERVICE_FINAL_SIGKILL ||
330 state == SERVICE_MAINTAINANCE ||
331 state == SERVICE_AUTO_RESTART)
332 service_notify_sockets(s);
334 log_debug("%s changed %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
336 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
339 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
351 if ((r = service_get_sockets(s, &set)) < 0)
354 SET_FOREACH(socket, set, i) {
358 if ((r = socket_collect_fds(socket, &cfds, &cn_fds)) < 0)
370 if (!(t = new(int, rn_fds+cn_fds))) {
376 memcpy(t, rfds, rn_fds);
377 memcpy(t+rn_fds, cfds, cn_fds);
382 rn_fds = rn_fds+cn_fds;
400 static int service_spawn(Service *s, ExecCommand *c, bool timeout, bool pass_fds, pid_t *_pid) {
411 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
415 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
418 unit_unwatch_timer(UNIT(s), &s->timer_watch);
420 if ((r = exec_spawn(c, &s->exec_context, fds, n_fds, &pid)) < 0)
423 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
424 /* FIXME: we need to do something here */
436 unit_unwatch_timer(UNIT(s), &s->timer_watch);
441 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
449 (s->restart == SERVICE_RESTART_ALWAYS ||
450 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
452 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
455 service_set_state(s, SERVICE_AUTO_RESTART);
457 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
462 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
463 service_enter_dead(s, false, false);
466 static void service_enter_signal(Service *s, ServiceState state, bool success);
468 static void service_enter_stop_post(Service *s, bool success) {
475 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)
480 service_set_state(s, SERVICE_STOP_POST);
482 if (!s->control_command)
483 service_enter_dead(s, true, true);
488 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
489 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
492 static void service_enter_signal(Service *s, ServiceState state, bool success) {
501 if (s->main_pid > 0 || s->control_pid > 0) {
504 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
507 if (s->main_pid > 0) {
508 if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
514 if (s->control_pid > 0) {
515 if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
525 service_set_state(s, state);
527 if (s->main_pid <= 0 && s->control_pid <= 0)
528 service_enter_dead(s, true, true);
533 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
537 service_set_state(s, state);
538 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
539 service_enter_stop_post(s, false);
541 service_enter_dead(s, false, true);
544 static void service_enter_stop(Service *s, bool success) {
551 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP]))
552 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
555 service_set_state(s, SERVICE_STOP);
557 if (!s->control_command)
558 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
563 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
564 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
567 static void service_enter_start_post(Service *s) {
571 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST]))
572 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
576 service_set_state(s, SERVICE_START_POST);
578 if (!s->control_command)
579 service_set_state(s, SERVICE_RUNNING);
584 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
585 service_enter_stop(s, false);
588 static void service_enter_start(Service *s) {
594 assert(s->exec_command[SERVICE_EXEC_START]);
595 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
597 if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
600 service_set_state(s, SERVICE_START);
602 if (s->type == SERVICE_SIMPLE) {
603 /* For simple services we immediately start
604 * the START_POST binaries. */
607 s->main_pid_known = true;
608 service_enter_start_post(s);
610 } else if (s->type == SERVICE_FORKING) {
612 /* For forking services we wait until the start
615 s->control_pid = pid;
616 s->control_command = s->exec_command[SERVICE_EXEC_START];
618 assert_not_reached("Unknown service type");
623 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
624 service_enter_stop(s, false);
627 static void service_enter_start_pre(Service *s) {
632 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]))
633 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
636 service_set_state(s, SERVICE_START_PRE);
638 if (!s->control_command)
639 service_enter_start(s);
644 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
645 service_enter_dead(s, false, true);
648 static void service_enter_restart(Service *s) {
652 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
655 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
656 service_enter_dead(s, true, false);
661 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
662 service_enter_dead(s, false, false);
665 static void service_enter_reload(Service *s) {
670 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]))
671 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
674 service_set_state(s, SERVICE_RELOAD);
676 if (!s->control_command)
677 service_set_state(s, SERVICE_RUNNING);
682 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
683 service_enter_stop(s, false);
686 static void service_run_next(Service *s, bool success) {
690 assert(s->control_command);
691 assert(s->control_command->command_next);
696 s->control_command = s->control_command->command_next;
698 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
704 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
706 if (s->state == SERVICE_STOP)
707 service_enter_stop_post(s, false);
708 else if (s->state == SERVICE_STOP_POST)
709 service_enter_dead(s, false, true);
711 service_enter_stop(s, false);
714 static int service_start(Unit *u) {
715 Service *s = SERVICE(u);
719 /* We cannot fulfill this request right now, try again later
721 if (s->state == SERVICE_STOP ||
722 s->state == SERVICE_STOP_SIGTERM ||
723 s->state == SERVICE_STOP_SIGKILL ||
724 s->state == SERVICE_STOP_POST ||
725 s->state == SERVICE_FINAL_SIGTERM ||
726 s->state == SERVICE_FINAL_SIGKILL)
730 if (s->state == SERVICE_START_PRE ||
731 s->state == SERVICE_START ||
732 s->state == SERVICE_START_POST)
735 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
738 s->main_pid_known = false;
740 service_enter_start_pre(s);
744 static int service_stop(Unit *u) {
745 Service *s = SERVICE(u);
749 if (s->state == SERVICE_START_PRE ||
750 s->state == SERVICE_START ||
751 s->state == SERVICE_START_POST ||
752 s->state == SERVICE_RELOAD)
755 if (s->state == SERVICE_AUTO_RESTART) {
756 service_set_state(s, SERVICE_DEAD);
760 assert(s->state == SERVICE_RUNNING);
762 service_enter_stop(s, true);
766 static int service_reload(Unit *u) {
767 Service *s = SERVICE(u);
771 assert(s->state == SERVICE_RUNNING);
773 service_enter_reload(s);
777 static bool service_can_reload(Unit *u) {
778 Service *s = SERVICE(u);
782 return !!s->exec_command[SERVICE_EXEC_RELOAD];
785 static UnitActiveState service_active_state(Unit *u) {
788 return state_translation_table[SERVICE(u)->state];
791 static int main_pid_good(Service *s) {
794 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
797 /* If we know the pid file, then lets just check if it is
799 if (s->main_pid_known)
800 return s->main_pid > 0;
802 /* We don't know the pid */
806 static bool control_pid_good(Service *s) {
809 return s->control_pid > 0;
812 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
813 Service *s = SERVICE(u);
819 success = code == CLD_EXITED && status == 0;
820 s->failure = s->failure || !success;
822 if (s->main_pid == pid) {
824 exec_status_fill(&s->main_exec_status, pid, code, status);
827 if (s->type == SERVICE_SIMPLE) {
828 assert(s->exec_command[SERVICE_EXEC_START]);
829 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
832 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
834 /* The service exited, so the service is officially
839 case SERVICE_START_POST:
842 /* Need to wait until the operation is
846 case SERVICE_RUNNING:
847 service_enter_stop(s, success);
850 case SERVICE_STOP_SIGTERM:
851 case SERVICE_STOP_SIGKILL:
853 if (!control_pid_good(s))
854 service_enter_stop_post(s, success);
856 /* If there is still a control process, wait for that first */
860 assert_not_reached("Uh, main process died at wrong time.");
863 } else if (s->control_pid == pid) {
864 assert(s->control_command);
866 exec_status_fill(&s->control_command->exec_status, pid, code, status);
869 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code(code), status);
871 /* If we are shutting things down anyway we
872 * don't care about failing commands. */
874 if (s->control_command->command_next &&
875 (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
877 /* There is another command to *
878 * execute, so let's do that. */
880 service_run_next(s, success);
883 /* No further commands for this step, so let's
884 * figure out what to do next */
886 log_debug("%s got final SIGCHLD for state %s", unit_id(u), state_string_table[s->state]);
890 case SERVICE_START_PRE:
892 service_enter_start(s);
894 service_enter_stop(s, false);
898 assert(s->type == SERVICE_FORKING);
900 /* Let's try to load the pid
901 * file here if we can. We
902 * ignore the return value,
903 * since the PID file might
904 * actually be created by a
905 * START_POST script */
909 service_load_pid_file(s);
911 service_enter_start_post(s);
913 service_enter_stop(s, false);
917 case SERVICE_START_POST:
918 if (success && s->pid_file && !s->main_pid_known) {
921 /* Hmm, let's see if we can
922 * load the pid now after the
923 * start-post scripts got
926 if ((r = service_load_pid_file(s)) < 0)
927 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
934 if (main_pid_good(s) != 0)
935 service_set_state(s, SERVICE_RUNNING);
937 service_enter_stop(s, true);
939 service_enter_stop(s, false);
944 if (main_pid_good(s) > 0)
945 /* Still not dead and we know the PID? Let's go hunting. */
946 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
948 service_enter_stop_post(s, success);
951 case SERVICE_STOP_SIGTERM:
952 case SERVICE_STOP_SIGKILL:
953 if (main_pid_good(s) <= 0)
954 service_enter_stop_post(s, success);
956 /* If there is still a service
957 * process around, wait until
958 * that one quit, too */
961 case SERVICE_STOP_POST:
962 case SERVICE_FINAL_SIGTERM:
963 case SERVICE_FINAL_SIGKILL:
964 service_enter_dead(s, success, true);
968 assert_not_reached("Uh, control process died at wrong time.");
972 assert_not_reached("Got SIGCHLD for unkown PID");
975 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
976 Service *s = SERVICE(u);
979 assert(elapsed == 1);
981 assert(w == &s->timer_watch);
985 case SERVICE_START_PRE:
987 case SERVICE_START_POST:
989 log_warning("%s operation timed out. Stopping.", unit_id(u));
990 service_enter_stop(s, false);
994 log_warning("%s stopping timed out. Terminating.", unit_id(u));
995 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
998 case SERVICE_STOP_SIGTERM:
999 log_warning("%s stopping timed out. Killing.", unit_id(u));
1000 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1003 case SERVICE_STOP_SIGKILL:
1004 /* Uh, wie sent a SIGKILL and it is still not gone?
1005 * Must be something we cannot kill, so let's just be
1006 * weirded out and continue */
1008 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1009 service_enter_stop_post(s, false);
1012 case SERVICE_STOP_POST:
1013 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1014 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1017 case SERVICE_FINAL_SIGTERM:
1018 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1019 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1022 case SERVICE_FINAL_SIGKILL:
1023 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1024 service_enter_dead(s, false, true);
1027 case SERVICE_AUTO_RESTART:
1028 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1029 service_enter_restart(s);
1033 assert_not_reached("Timeout at wrong time.");
1037 const UnitVTable service_vtable = {
1038 .suffix = ".service",
1040 .init = service_init,
1041 .done = service_done,
1043 .dump = service_dump,
1045 .start = service_start,
1046 .stop = service_stop,
1047 .reload = service_reload,
1049 .can_reload = service_can_reload,
1051 .active_state = service_active_state,
1053 .sigchld_event = service_sigchld_event,
1054 .timer_event = service_timer_event,