+
+fail:
+ if (timeout)
+ name_unwatch_timer(NAME(s), &s->timer_id);
+
+ return r;
+}
+
+static void service_enter_dead(Service *s, bool success, bool allow_restart) {
+ int r;
+ assert(s);
+
+ if (!success)
+ s->failure = true;
+
+ if (allow_restart &&
+ (s->restart == SERVICE_RESTART_ALWAYS ||
+ (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
+
+ if ((r = name_watch_timer(NAME(s), s->restart_usec, &s->timer_id)) < 0)
+ goto fail;
+
+ service_set_state(s, SERVICE_AUTO_RESTART);
+ } else
+ service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
+
+ return;
+
+fail:
+ log_warning("%s failed to run install restart timer: %s", name_id(NAME(s)), strerror(-r));
+ service_enter_dead(s, false, false);
+}
+
+static void service_enter_signal(Service *s, ServiceState state, bool success);
+
+static void service_enter_stop_post(Service *s, bool success) {
+ int r;
+ assert(s);
+
+ if (!success)
+ s->failure = true;
+
+ if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
+
+ if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ goto fail;
+
+ service_set_state(s, SERVICE_STOP_POST);
+ } else
+ service_enter_dead(s, true, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to run stop executable: %s", name_id(NAME(s)), strerror(-r));
+ service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
+}
+
+static void service_enter_signal(Service *s, ServiceState state, bool success) {
+ int r;
+ bool sent = false;
+
+ assert(s);
+
+ if (!success)
+ s->failure = true;
+
+ if (s->main_pid > 0 || s->control_pid > 0) {
+ int sig;
+
+ sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
+
+ r = 0;
+ if (s->main_pid > 0) {
+ if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
+ r = -errno;
+ else
+ sent = true;
+ }
+
+ if (s->control_pid > 0) {
+ if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
+ r = -errno;
+ else
+ sent = true;
+ }
+
+ if (r < 0)
+ goto fail;
+
+ service_set_state(s, state);
+ } else
+ service_enter_dead(s, true, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to kill processes: %s", name_id(NAME(s)), strerror(-r));
+
+ if (sent) {
+ s->failure = true;
+ service_set_state(s, state);
+ } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
+ service_enter_stop_post(s, false);
+ else
+ service_enter_dead(s, false, true);
+}
+
+static void service_enter_stop(Service *s, bool success) {
+ int r;
+ assert(s);
+
+ if (!success)
+ s->failure = true;
+
+ if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
+
+ if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ goto fail;
+
+ service_set_state(s, SERVICE_STOP);
+ } else
+ service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to run stop executable: %s", name_id(NAME(s)), strerror(-r));
+ service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
+}
+
+static void service_enter_start_post(Service *s) {
+ int r;
+ assert(s);
+
+ if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
+
+ if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ goto fail;
+
+ service_set_state(s, SERVICE_START_POST);
+ } else
+ service_set_state(s, SERVICE_RUNNING);
+
+ return;
+
+fail:
+ log_warning("%s failed to run start-post executable: %s", name_id(NAME(s)), strerror(-r));
+ service_enter_stop(s, false);
+}
+
+static void service_enter_start(Service *s) {
+ pid_t pid;
+ int r;
+
+ assert(s);
+
+ assert(s->exec_command[SERVICE_EXEC_START]);
+ assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
+
+ if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, &pid)) < 0)
+ goto fail;
+
+ if (s->type == SERVICE_SIMPLE) {
+ /* For simple services we immediately start
+ * the START_POST binaries. */
+
+ s->main_pid = pid;
+ s->main_pid_known = true;
+ service_enter_start_post(s);
+
+ } else if (s->type == SERVICE_FORKING) {
+
+ /* For forking services we wait until the start
+ * process exited. */
+
+ s->control_pid = pid;
+ s->control_command = s->exec_command[SERVICE_EXEC_START];
+ service_set_state(s, SERVICE_START);
+ } else
+ assert_not_reached("Unknown service type");
+
+ return;
+
+fail:
+ log_warning("%s failed to run start exectuable: %s", name_id(NAME(s)), strerror(-r));
+ service_enter_stop(s, false);
+}
+
+static void service_enter_start_pre(Service *s) {
+ int r;
+
+ assert(s);
+
+ if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
+
+ if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ goto fail;
+
+ service_set_state(s, SERVICE_START_PRE);
+ } else
+ service_enter_start(s);
+
+ return;
+
+fail:
+ log_warning("%s failed to run start-pre executable: %s", name_id(NAME(s)), strerror(-r));
+ service_enter_dead(s, false, true);
+}
+
+static void service_enter_restart(Service *s) {
+ int r;
+ assert(s);
+
+ if ((r = manager_add_job(NAME(s)->meta.manager, JOB_START, NAME(s), JOB_FAIL, false, NULL)) < 0)
+ goto fail;
+
+ log_debug("%s scheduled restart job.", name_id(NAME(s)));
+ service_enter_dead(s, true, false);
+ return;
+
+fail:
+
+ log_warning("%s failed to schedule restart job: %s", name_id(NAME(s)), strerror(-r));
+ service_enter_dead(s, false, false);
+}
+
+static void service_enter_reload(Service *s) {
+ int r;
+
+ assert(s);
+
+ if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
+
+ if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ goto fail;
+
+ service_set_state(s, SERVICE_RELOAD);
+ } else
+ service_set_state(s, SERVICE_RUNNING);
+
+ return;
+
+fail:
+ log_warning("%s failed to run reload executable: %s", name_id(NAME(s)), strerror(-r));
+ service_enter_stop(s, false);
+}
+
+static void service_run_next(Service *s, bool success) {
+ int r;
+
+ assert(s);
+ assert(s->control_command);
+ assert(s->control_command->command_next);
+
+ if (!success)
+ s->failure = true;
+
+ s->control_command = s->control_command->command_next;
+
+ if ((r = service_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ goto fail;
+
+ return;
+
+fail:
+ log_warning("%s failed to run spawn next executable: %s", name_id(NAME(s)), strerror(-r));
+
+ if (s->state == SERVICE_STOP)
+ service_enter_stop_post(s, false);
+ else if (s->state == SERVICE_STOP_POST)
+ service_enter_dead(s, false, true);
+ else
+ service_enter_stop(s, false);