+static void socket_set_state(Socket *s, SocketState state) {
+ SocketState old_state;
+ assert(s);
+
+ old_state = s->state;
+ s->state = state;
+
+ if (state != SOCKET_START_PRE &&
+ state != SOCKET_START_POST &&
+ state != SOCKET_STOP_PRE &&
+ state != SOCKET_STOP_PRE_SIGTERM &&
+ state != SOCKET_STOP_PRE_SIGKILL &&
+ state != SOCKET_STOP_POST &&
+ state != SOCKET_STOP_POST_SIGTERM &&
+ state != SOCKET_STOP_POST_SIGKILL) {
+ unit_unwatch_timer(UNIT(s), &s->timer_watch);
+
+ if (s->control_pid > 0) {
+ unit_unwatch_pid(UNIT(s), s->control_pid);
+ s->control_pid = 0;
+ }
+
+ s->control_command = NULL;
+ }
+
+ if (state != SOCKET_START_POST &&
+ state != SOCKET_LISTENING &&
+ state != SOCKET_RUNNING &&
+ state != SOCKET_STOP_PRE &&
+ state != SOCKET_STOP_PRE_SIGTERM &&
+ state != SOCKET_STOP_PRE_SIGKILL)
+ socket_close_fds(s);
+
+ if (state != SOCKET_LISTENING)
+ socket_unwatch_fds(s);
+
+ if (state != old_state)
+ log_debug("%s changed %s ā %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
+
+ unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
+}
+
+static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
+ pid_t pid;
+ int r;
+
+ assert(s);
+ assert(c);
+ assert(_pid);
+
+ if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
+ goto fail;
+
+ if ((r = exec_spawn(c,
+ &s->exec_context,
+ NULL, 0,
+ true,
+ true,
+ UNIT(s)->meta.cgroup_bondings,
+ &pid)) < 0)
+ goto fail;
+
+ if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
+ /* FIXME: we need to do something here */
+ goto fail;
+
+ *_pid = pid;
+
+ return 0;
+
+fail:
+ unit_unwatch_timer(UNIT(s), &s->timer_watch);
+
+ return r;
+}
+
+static void socket_enter_dead(Socket *s, bool success) {
+ assert(s);
+
+ if (!success)
+ s->failure = true;
+
+ socket_set_state(s, s->failure ? SOCKET_MAINTAINANCE : SOCKET_DEAD);
+}
+
+static void socket_enter_stop_post(Socket *s, bool success) {
+ int r;
+ assert(s);
+
+ if (!success)
+ s->failure = true;
+
+ if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST]))
+ if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
+ goto fail;
+
+ socket_set_state(s, SOCKET_STOP_POST);
+
+ if (!s->control_command)
+ socket_enter_dead(s, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s)), strerror(-r));
+ socket_enter_dead(s, false);
+}
+
+static void socket_enter_signal(Socket *s, SocketState state, bool success) {
+ int r;
+
+ assert(s);
+
+ if (!success)
+ s->failure = true;
+
+ if (s->control_pid > 0) {
+ int sig;
+ bool sent = false;
+
+ sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_POST_SIGTERM) ? SIGTERM : SIGKILL;
+
+ if (s->kill_mode == KILL_CONTROL_GROUP) {
+
+ if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
+ if (r != -EAGAIN && r != -ESRCH)
+ goto fail;
+ } else
+ sent = true;
+ }
+
+ if (!sent)
+ if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH) {
+ r = -errno;
+ goto fail;
+ }
+ }
+
+ socket_set_state(s, state);
+
+ if (s->control_pid <= 0)
+ socket_enter_dead(s, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
+
+ if (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_PRE_SIGKILL)
+ socket_enter_stop_post(s, false);
+ else
+ socket_enter_dead(s, false);
+}
+
+static void socket_enter_stop_pre(Socket *s, bool success) {
+ int r;
+ assert(s);
+
+ if (!success)
+ s->failure = true;
+
+ if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE]))
+ if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0)
+ goto fail;
+
+ socket_set_state(s, SOCKET_STOP_PRE);
+
+ if (!s->control_command)
+ socket_enter_stop_post(s, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to run stop-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
+ socket_enter_stop_post(s, false);
+}
+
+static void socket_enter_listening(Socket *s) {
+ int r;
+ assert(s);
+
+ if ((r = socket_watch_fds(s)) < 0) {
+ log_warning("%s failed to watch sockets: %s", unit_id(UNIT(s)), strerror(-r));
+ goto fail;
+ }
+
+ socket_set_state(s, SOCKET_LISTENING);
+ return;
+
+fail:
+ socket_enter_stop_pre(s, false);
+}
+
+static void socket_enter_start_post(Socket *s) {
+ int r;
+ assert(s);
+
+ if ((r = socket_open_fds(s)) < 0) {
+ log_warning("%s failed to listen on sockets: %s", unit_id(UNIT(s)), strerror(-r));
+ goto fail;
+ }
+
+ if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST]))
+ if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) {
+ log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
+ goto fail;
+ }
+
+ socket_set_state(s, SOCKET_START_POST);
+
+ if (!s->control_command)
+ socket_enter_listening(s);