+ 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, false, &s->control_pid)) < 0)
+ goto fail;
+
+
+ service_set_state(s, SERVICE_STOP_POST);
+
+ if (!s->control_command)
+ service_enter_dead(s, true, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to run stop executable: %s", unit_id(UNIT(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);
+
+ if (s->main_pid <= 0 && s->control_pid <= 0)
+ service_enter_dead(s, true, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to kill processes: %s", unit_id(UNIT(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, false, &s->control_pid)) < 0)
+ goto fail;
+
+ service_set_state(s, SERVICE_STOP);
+
+ if (!s->control_command)
+ service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to run stop executable: %s", unit_id(UNIT(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, false, &s->control_pid)) < 0)
+ goto fail;
+
+
+ service_set_state(s, SERVICE_START_POST);
+
+ if (!s->control_command)
+ service_set_state(s, SERVICE_RUNNING);
+
+ return;
+
+fail:
+ log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(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, true, &pid)) < 0)
+ goto fail;
+
+ service_set_state(s, SERVICE_START);
+
+ 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];
+ } else
+ assert_not_reached("Unknown service type");
+
+ return;
+
+fail:
+ log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(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, false, &s->control_pid)) < 0)
+ goto fail;
+
+ service_set_state(s, SERVICE_START_PRE);
+
+ if (!s->control_command)
+ service_enter_start(s);
+
+ return;
+
+fail:
+ log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(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(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
+ goto fail;
+
+ log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
+ service_enter_dead(s, true, false);
+ return;
+
+fail:
+
+ log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(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, false, &s->control_pid)) < 0)
+ goto fail;
+
+ service_set_state(s, SERVICE_RELOAD);
+
+ if (!s->control_command)
+ service_set_state(s, SERVICE_RUNNING);
+
+ return;
+
+fail:
+ log_warning("%s failed to run reload executable: %s", unit_id(UNIT(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, false, &s->control_pid)) < 0)
+ goto fail;
+
+ return;
+
+fail:
+ log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(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);
+}
+
+static int service_start(Unit *u) {
+ Service *s = SERVICE(u);
+
+ assert(s);
+
+ /* We cannot fulfill this request right now, try again later
+ * please! */
+ if (s->state == SERVICE_STOP ||
+ s->state == SERVICE_STOP_SIGTERM ||
+ s->state == SERVICE_STOP_SIGKILL ||
+ s->state == SERVICE_STOP_POST ||
+ s->state == SERVICE_FINAL_SIGTERM ||
+ s->state == SERVICE_FINAL_SIGKILL)