+static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
+ Service *s = SERVICE(u);
+ bool success;
+
+ assert(s);
+ assert(pid >= 0);
+
+ success = code == CLD_EXITED && status == 0;
+ s->failure = s->failure || !success;
+
+ if (s->main_pid == pid) {
+
+ exec_status_fill(&s->main_exec_status, pid, code, status);
+ s->main_pid = 0;
+
+ if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
+ assert(s->exec_command[SERVICE_EXEC_START]);
+ s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
+ }
+
+ log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
+
+ /* The service exited, so the service is officially
+ * gone. */
+
+ switch (s->state) {
+
+ case SERVICE_START_POST:
+ case SERVICE_RELOAD:
+ case SERVICE_STOP:
+ /* Need to wait until the operation is
+ * done */
+ break;
+
+ case SERVICE_START:
+ assert(s->type == SERVICE_FINISH);
+
+ /* This was our main goal, so let's go on */
+ if (success)
+ service_enter_start_post(s);
+ else
+ service_enter_stop(s, false);
+ break;
+
+ case SERVICE_RUNNING:
+ service_enter_stop(s, success);
+ break;
+
+ case SERVICE_STOP_SIGTERM:
+ case SERVICE_STOP_SIGKILL:
+
+ if (!control_pid_good(s))
+ service_enter_stop_post(s, success);
+
+ /* If there is still a control process, wait for that first */
+ break;
+
+ default:
+ assert_not_reached("Uh, main process died at wrong time.");
+ }
+
+ } else if (s->control_pid == pid) {
+ assert(s->control_command);
+
+ exec_status_fill(&s->control_command->exec_status, pid, code, status);
+ s->control_pid = 0;
+
+ log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
+
+ /* If we are shutting things down anyway we
+ * don't care about failing commands. */
+
+ if (s->control_command->command_next &&
+ (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
+
+ /* There is another command to *
+ * execute, so let's do that. */
+
+ service_run_next(s, success);
+
+ else {
+ /* No further commands for this step, so let's
+ * figure out what to do next */
+
+ log_debug("%s got final SIGCHLD for state %s", unit_id(u), service_state_to_string(s->state));
+
+ switch (s->state) {
+
+ case SERVICE_START_PRE:
+ if (success)
+ service_enter_start(s);
+ else
+ service_enter_stop(s, false);
+ break;
+
+ case SERVICE_START:
+ assert(s->type == SERVICE_FORKING);
+
+ /* Let's try to load the pid
+ * file here if we can. We
+ * ignore the return value,
+ * since the PID file might
+ * actually be created by a
+ * START_POST script */
+
+ if (success) {
+ if (s->pid_file)
+ service_load_pid_file(s);
+
+ service_enter_start_post(s);
+ } else
+ service_enter_stop(s, false);
+
+ break;
+
+ case SERVICE_START_POST:
+ if (success && s->pid_file && !s->main_pid_known) {
+ int r;
+
+ /* Hmm, let's see if we can
+ * load the pid now after the
+ * start-post scripts got
+ * executed. */
+
+ if ((r = service_load_pid_file(s)) < 0)
+ log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
+ }
+
+ /* Fall through */
+
+ case SERVICE_RELOAD:
+ if (success) {
+ if (main_pid_good(s) != 0)
+ service_set_state(s, SERVICE_RUNNING);
+ else
+ service_enter_stop(s, true);
+ } else
+ service_enter_stop(s, false);
+
+ break;
+
+ case SERVICE_STOP:
+ if (main_pid_good(s) > 0)
+ /* Still not dead and we know the PID? Let's go hunting. */
+ service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
+ else
+ service_enter_stop_post(s, success);
+ break;
+
+ case SERVICE_STOP_SIGTERM:
+ case SERVICE_STOP_SIGKILL:
+ if (main_pid_good(s) <= 0)
+ service_enter_stop_post(s, success);
+
+ /* If there is still a service
+ * process around, wait until
+ * that one quit, too */
+ break;
+
+ case SERVICE_STOP_POST:
+ case SERVICE_FINAL_SIGTERM:
+ case SERVICE_FINAL_SIGKILL:
+ service_enter_dead(s, success, true);
+ break;
+
+ default:
+ assert_not_reached("Uh, control process died at wrong time.");
+ }
+ }
+ } else
+ assert_not_reached("Got SIGCHLD for unkown PID");
+}
+
+static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
+ Service *s = SERVICE(u);
+
+ assert(s);
+ assert(elapsed == 1);
+
+ assert(w == &s->timer_watch);
+
+ switch (s->state) {
+
+ case SERVICE_START_PRE:
+ case SERVICE_START:
+ case SERVICE_START_POST:
+ case SERVICE_RELOAD:
+ log_warning("%s operation timed out. Stopping.", unit_id(u));
+ service_enter_stop(s, false);
+ break;
+
+ case SERVICE_STOP:
+ log_warning("%s stopping timed out. Terminating.", unit_id(u));
+ service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
+ break;
+
+ case SERVICE_STOP_SIGTERM:
+ log_warning("%s stopping timed out. Killing.", unit_id(u));
+ service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
+ break;
+
+ case SERVICE_STOP_SIGKILL:
+ /* Uh, wie sent a SIGKILL and it is still not gone?
+ * Must be something we cannot kill, so let's just be
+ * weirded out and continue */
+
+ log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
+ service_enter_stop_post(s, false);
+ break;
+
+ case SERVICE_STOP_POST:
+ log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
+ service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
+ break;
+
+ case SERVICE_FINAL_SIGTERM:
+ log_warning("%s stopping timed out (2). Killing.", unit_id(u));
+ service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
+ break;
+
+ case SERVICE_FINAL_SIGKILL:
+ log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
+ service_enter_dead(s, false, true);
+ break;
+
+ case SERVICE_AUTO_RESTART:
+ log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
+ service_enter_restart(s);
+ break;
+
+ default:
+ assert_not_reached("Timeout at wrong time.");
+ }
+}
+
+static const char* const service_state_table[_SERVICE_STATE_MAX] = {
+ [SERVICE_DEAD] = "dead",
+ [SERVICE_START_PRE] = "start-pre",
+ [SERVICE_START] = "start",
+ [SERVICE_START_POST] = "start-post",
+ [SERVICE_RUNNING] = "running",
+ [SERVICE_RELOAD] = "reload",
+ [SERVICE_STOP] = "stop",
+ [SERVICE_STOP_SIGTERM] = "stop-sigterm",
+ [SERVICE_STOP_SIGKILL] = "stop-sigkill",
+ [SERVICE_STOP_POST] = "stop-post",
+ [SERVICE_FINAL_SIGTERM] = "final-sigterm",
+ [SERVICE_FINAL_SIGKILL] = "final-sigkill",
+ [SERVICE_MAINTAINANCE] = "maintainance",
+ [SERVICE_AUTO_RESTART] = "auto-restart",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
+
+static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
+ [SERVICE_ONCE] = "once",
+ [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
+ [SERVICE_RESTART_ALWAYS] = "restart-on-failure",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
+
+static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
+ [SERVICE_FORKING] = "forking",
+ [SERVICE_SIMPLE] = "simple",
+ [SERVICE_FINISH] = "finish"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
+
+static const char* const service_exec_command_table[_SERVICE_EXEC_MAX] = {
+ [SERVICE_EXEC_START_PRE] = "ExecStartPre",
+ [SERVICE_EXEC_START] = "ExecStart",
+ [SERVICE_EXEC_START_POST] = "ExecStartPost",
+ [SERVICE_EXEC_RELOAD] = "ExecReload",
+ [SERVICE_EXEC_STOP] = "ExecStop",
+ [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
+
+const UnitVTable service_vtable = {