summaryrefslogtreecommitdiff
path: root/socket.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2010-01-26 04:18:44 +0100
committerLennart Poettering <lennart@poettering.net>2010-01-26 04:18:44 +0100
commit034c6ed7da5e44bfdde5a5d0da75f7b7a59953b8 (patch)
treeea59315075d3ebda2de695b1bd0621d79fa9e987 /socket.c
parent9152c765065184d0c1267ed2499e3fe4cac53755 (diff)
first attempt at proper service/socket logic
Diffstat (limited to 'socket.c')
-rw-r--r--socket.c589
1 files changed, 508 insertions, 81 deletions
diff --git a/socket.c b/socket.c
index ac0ea124b4..a8a1914249 100644
--- a/socket.c
+++ b/socket.c
@@ -6,6 +6,7 @@
#include <errno.h>
#include <fcntl.h>
#include <sys/poll.h>
+#include <signal.h>
#include "name.h"
#include "socket.h"
@@ -18,17 +19,75 @@ static const NameActiveState state_table[_SOCKET_STATE_MAX] = {
[SOCKET_LISTENING] = NAME_ACTIVE,
[SOCKET_RUNNING] = NAME_ACTIVE,
[SOCKET_STOP_PRE] = NAME_DEACTIVATING,
+ [SOCKET_STOP_PRE_SIGTERM] = NAME_DEACTIVATING,
+ [SOCKET_STOP_PRE_SIGKILL] = NAME_DEACTIVATING,
[SOCKET_STOP_POST] = NAME_DEACTIVATING,
+ [SOCKET_STOP_POST_SIGTERM] = NAME_DEACTIVATING,
+ [SOCKET_STOP_POST_SIGKILL] = NAME_DEACTIVATING,
[SOCKET_MAINTAINANCE] = NAME_INACTIVE,
};
-static int socket_load(Name *n) {
+static int socket_init(Name *n) {
Socket *s = SOCKET(n);
+ char *t;
+ int r;
+
+ /* First, reset everything to the defaults, in case this is a
+ * reload */
- exec_context_defaults(&s->exec_context);
+ s->bind_ipv6_only = false;
s->backlog = SOMAXCONN;
+ s->timeout_usec = DEFAULT_TIMEOUT_USEC;
+ exec_context_init(&s->exec_context);
+
+ if ((r = name_load_fragment_and_dropin(n)) < 0)
+ return r;
+
+ if (!(t = name_change_suffix(name_id(n), ".service")))
+ return -ENOMEM;
+
+ r = manager_load_name(n->meta.manager, t, (Name**) &s->service);
+ free(t);
- return name_load_fragment_and_dropin(n);
+ if (r < 0)
+ return r;
+
+ if ((r = set_ensure_allocated(n->meta.dependencies + NAME_BEFORE, trivial_hash_func, trivial_compare_func)) < 0)
+ return r;
+
+ if ((r = set_put(n->meta.dependencies[NAME_BEFORE], s->service)) < 0)
+ return r;
+
+ return 0;
+}
+
+static void socket_done(Name *n) {
+ Socket *s = SOCKET(n);
+ SocketPort *p;
+
+ assert(s);
+
+ while ((p = s->ports)) {
+ LIST_REMOVE(SocketPort, port, s->ports, p);
+
+ if (p->fd >= 0)
+ close_nointr(p->fd);
+ free(p->path);
+ free(p);
+ }
+
+ exec_context_done(&s->exec_context);
+ exec_command_free_array(s->exec_command, _SOCKET_EXEC_MAX);
+ s->control_command = NULL;
+
+ if (s->control_pid > 0) {
+ name_unwatch_pid(n, s->control_pid);
+ s->control_pid = 0;
+ }
+
+ s->service = NULL;
+
+ name_unwatch_timer(n, &s->timer_id);
}
static const char* listen_lookup(int type) {
@@ -40,7 +99,7 @@ static const char* listen_lookup(int type) {
else if (type == SOCK_SEQPACKET)
return "ListenSequentialPacket";
- assert_not_reached("Unkown socket type");
+ assert_not_reached("Unknown socket type");
return NULL;
}
@@ -53,7 +112,11 @@ static void socket_dump(Name *n, FILE *f, const char *prefix) {
[SOCKET_LISTENING] = "listening",
[SOCKET_RUNNING] = "running",
[SOCKET_STOP_PRE] = "stop-pre",
+ [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm",
+ [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill",
[SOCKET_STOP_POST] = "stop-post",
+ [SOCKET_STOP_POST_SIGTERM] = "stop-post-sigterm",
+ [SOCKET_STOP_POST_SIGKILL] = "stop-post-sigkill",
[SOCKET_MAINTAINANCE] = "maintainance"
};
@@ -78,7 +141,7 @@ static void socket_dump(Name *n, FILE *f, const char *prefix) {
prefix, yes_no(s->bind_ipv6_only),
prefix, s->backlog);
- LIST_FOREACH(p, s->ports) {
+ LIST_FOREACH(port, p, s->ports) {
if (p->type == SOCKET_SOCKET) {
const char *t;
@@ -101,27 +164,17 @@ static void socket_dump(Name *n, FILE *f, const char *prefix) {
for (c = 0; c < _SOCKET_EXEC_MAX; c++) {
ExecCommand *i;
- LIST_FOREACH(i, s->exec_command[c])
+ LIST_FOREACH(command, i, s->exec_command[c])
fprintf(f, "%s%s: %s\n", prefix, command_table[c], i->path);
}
}
-static void socket_set_state(Socket *s, SocketState state) {
- SocketState old_state;
- assert(s);
-
- old_state = s->state;
- s->state = state;
-
- name_notify(NAME(s), state_table[old_state], state_table[s->state]);
-}
-
-static void close_fds(Socket *s) {
+static void socket_close_fds(Socket *s) {
SocketPort *p;
assert(s);
- LIST_FOREACH(p, s->ports) {
+ LIST_FOREACH(port, p, s->ports) {
if (p->fd < 0)
continue;
@@ -132,30 +185,16 @@ static void close_fds(Socket *s) {
}
}
-static int socket_start(Name *n) {
- Socket *s = SOCKET(n);
+static int socket_open_fds(Socket *s) {
SocketPort *p;
int r;
assert(s);
- if (s->state == SOCKET_START_PRE ||
- s->state == SOCKET_START_POST)
- return 0;
-
- if (s->state == SOCKET_LISTENING ||
- s->state == SOCKET_RUNNING)
- return -EALREADY;
-
- if (s->state == SOCKET_STOP_PRE ||
- s->state == SOCKET_STOP_POST)
- return -EAGAIN;
-
- assert(s->state == SOCKET_DEAD || s->state == SOCKET_MAINTAINANCE);
-
- LIST_FOREACH(p, s->ports) {
+ LIST_FOREACH(port, p, s->ports) {
- assert(p->fd < 0);
+ if (p->fd >= 0)
+ continue;
if (p->type == SOCKET_SOCKET) {
@@ -188,46 +227,352 @@ static int socket_start(Name *n) {
goto rollback;
}
}
+ }
+
+ return 0;
+
+rollback:
+ socket_close_fds(s);
+ return r;
+}
+
+static void socket_unwatch_fds(Socket *s) {
+ SocketPort *p;
- if ((r = name_watch_fd(n, p->fd, POLLIN)) < 0)
- goto rollback;
+ assert(s);
+
+ LIST_FOREACH(port, p, s->ports) {
+ if (p->fd < 0)
+ continue;
+
+ name_unwatch_fd(NAME(s), p->fd);
}
+}
+
+static int socket_watch_fds(Socket *s) {
+ SocketPort *p;
+ int r;
+
+ assert(s);
- socket_set_state(s, SOCKET_LISTENING);
+ LIST_FOREACH(port, p, s->ports) {
+ if (p->fd < 0)
+ continue;
+
+ if ((r = name_watch_fd(NAME(s), p->fd, POLLIN)) < 0)
+ goto fail;
+ }
return 0;
-rollback:
- close_fds(s);
+fail:
+ socket_unwatch_fds(s);
+ return r;
+}
+
+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)
+ name_unwatch_timer(NAME(s), &s->timer_id);
+
+ 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)
+ if (s->control_pid >= 0) {
+ name_unwatch_pid(NAME(s), s->control_pid);
+ s->control_pid = 0;
+ }
+
+ if (state != SOCKET_START_PRE &&
+ state != SOCKET_START_POST &&
+ state != SOCKET_STOP_PRE &&
+ state != SOCKET_STOP_POST)
+ 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);
+
+ name_notify(NAME(s), state_table[old_state], state_table[s->state]);
+}
+
+static int socket_spawn(Socket *s, ExecCommand *c, bool timeout, pid_t *_pid) {
+ pid_t pid;
+ int r;
+
+ assert(s);
+ assert(c);
+ assert(_pid);
+
+ if (timeout) {
+ if ((r = name_watch_timer(NAME(s), s->timeout_usec, &s->timer_id)) < 0)
+ goto fail;
+ } else
+ name_unwatch_timer(NAME(s), &s->timer_id);
+
+ if ((r = exec_spawn(c, &s->exec_context, NULL, 0, &pid)) < 0)
+ goto fail;
+
+ if ((r = name_watch_pid(NAME(s), pid)) < 0)
+ /* FIXME: we need to do something here */
+ goto fail;
- socket_set_state(s, SOCKET_MAINTAINANCE);
+ *_pid = pid;
+
+ return 0;
+
+fail:
+ if (timeout)
+ name_unwatch_timer(NAME(s), &s->timer_id);
return r;
}
-static int socket_stop(Name *n) {
+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, true, &s->control_pid)) < 0)
+ goto fail;
+
+ socket_set_state(s, SOCKET_STOP_POST);
+ } else
+ socket_enter_dead(s, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to run stop-post executable: %s", name_id(NAME(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;
+
+ sig = (state == SOCKET_STOP_PRE_SIGTERM || state == SOCKET_STOP_POST_SIGTERM) ? SIGTERM : SIGKILL;
+
+ if (kill(s->control_pid, sig) < 0 && errno != ESRCH) {
+ r = -errno;
+ goto fail;
+ }
+
+ socket_set_state(s, state);
+ } else
+ socket_enter_dead(s, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to kill processes: %s", name_id(NAME(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, true, &s->control_pid)) < 0)
+ goto fail;
+
+ socket_set_state(s, SOCKET_STOP_PRE);
+ } else
+ socket_enter_stop_post(s, true);
+
+ return;
+
+fail:
+ log_warning("%s failed to run stop-pre executable: %s", name_id(NAME(s)), strerror(-r));
+ socket_enter_stop_post(s, false);
+}
+
+static void socket_enter_start_post(Socket *s) {
+ int r;
+ assert(s);
+
+ if ((r = socket_open_fds(s)) < 0 ||
+ (r = socket_watch_fds(s)) < 0) {
+ log_warning("%s failed to listen on sockets: %s", name_id(NAME(s)), strerror(-r));
+ goto fail;
+ }
+
+ if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) {
+
+ if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0) {
+ log_warning("%s failed to run start-post executable: %s", name_id(NAME(s)), strerror(-r));
+ goto fail;
+ }
+
+ socket_set_state(s, SOCKET_START_POST);
+ } else
+ socket_set_state(s, SOCKET_LISTENING);
+
+ return;
+
+fail:
+ socket_enter_stop_pre(s, false);
+}
+
+static void socket_enter_start_pre(Socket *s) {
+ int r;
+ assert(s);
+
+ if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) {
+
+ if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ goto fail;
+
+ socket_set_state(s, SOCKET_START_PRE);
+ } else
+ socket_enter_start_post(s);
+
+ return;
+
+fail:
+ log_warning("%s failed to run start-pre exectuable: %s", name_id(NAME(s)), strerror(-r));
+ socket_enter_dead(s, false);
+}
+
+static void socket_enter_running(Socket *s) {
+ int r;
+
+ assert(s);
+
+ if ((r = manager_add_job(NAME(s)->meta.manager, JOB_START, NAME(s->service), JOB_REPLACE, true, NULL)) < 0)
+ goto fail;
+
+ socket_set_state(s, SOCKET_RUNNING);
+ return;
+
+fail:
+ log_warning("%s failed to queue socket startup job: %s", name_id(NAME(s)), strerror(-r));
+ socket_enter_dead(s, false);
+}
+
+static void socket_run_next(Socket *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 = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
+ goto fail;
+
+ return;
+
+fail:
+ if (s->state == SOCKET_STOP_PRE)
+ socket_enter_stop_post(s, false);
+ else if (s->state == SOCKET_STOP_POST)
+ socket_enter_dead(s, false);
+ else
+ socket_enter_stop_pre(s, false);
+}
+
+static int socket_start(Name *n) {
Socket *s = SOCKET(n);
assert(s);
+ /* We cannot fulfill this request right now, try again later
+ * please! */
+ if (s->state == SOCKET_STOP_PRE ||
+ s->state == SOCKET_STOP_PRE_SIGKILL ||
+ s->state == SOCKET_STOP_PRE_SIGTERM ||
+ s->state == SOCKET_STOP_POST ||
+ s->state == SOCKET_STOP_POST_SIGTERM ||
+ s->state == SOCKET_STOP_POST_SIGKILL)
+ return -EAGAIN;
+
if (s->state == SOCKET_START_PRE ||
s->state == SOCKET_START_POST)
- return -EAGAIN;
+ return 0;
- if (s->state == SOCKET_DEAD ||
- s->state == SOCKET_MAINTAINANCE)
- return -EALREADY;
+ /* Cannot run this without the service being around */
+ if (s->service->meta.load_state != NAME_LOADED)
+ return -ENOENT;
- if (s->state == SOCKET_STOP_PRE ||
- s->state == SOCKET_STOP_POST)
- return 0;
+ assert(s->state == SOCKET_DEAD || s->state == SOCKET_MAINTAINANCE);
- assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING);
+ s->failure = false;
+ socket_enter_start_pre(s);
+ return 0;
+}
- close_fds(s);
+static int socket_stop(Name *n) {
+ Socket *s = SOCKET(n);
+
+ assert(s);
+
+ /* We cannot fulfill this request right now, try again later
+ * please! */
+ if (s->state == SOCKET_START_PRE ||
+ s->state == SOCKET_START_POST)
+ return -EAGAIN;
- socket_set_state(s, SOCKET_DEAD);
+ assert(s->state == SOCKET_LISTENING || s->state == SOCKET_RUNNING);
+ socket_enter_stop_pre(s, true);
return 0;
}
@@ -240,58 +585,140 @@ static NameActiveState socket_active_state(Name *n) {
static void socket_fd_event(Name *n, int fd, uint32_t events) {
Socket *s = SOCKET(n);
- assert(n);
-
- if (events != POLLIN)
- goto fail;
+ assert(s);
- log_info("POLLIN on %s", name_id(n));
+ log_info("Incoming traffic on %s", name_id(n));
- return;
+ if (events != POLLIN)
+ socket_enter_stop_pre(s, false);
-fail:
- close_fds(s);
- socket_set_state(s, SOCKET_MAINTAINANCE);
+ socket_enter_running(s);
}
-static void socket_free_hook(Name *n) {
- SocketExecCommand c;
+static void socket_sigchld_event(Name *n, pid_t pid, int code, int status) {
Socket *s = SOCKET(n);
- SocketPort *p;
+ bool success;
assert(s);
+ assert(pid >= 0);
- while ((p = s->ports)) {
- LIST_REMOVE(SocketPort, s->ports, p);
+ success = code == CLD_EXITED || status == 0;
+ s->failure = s->failure || !success;
- if (p->fd >= 0)
- close_nointr(p->fd);
- free(p->path);
- free(p);
- }
+ assert(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", name_id(n), sigchld_code(code), status);
+
+ if (s->control_command->command_next &&
+ (success || (s->state == SOCKET_EXEC_STOP_PRE || s->state == SOCKET_EXEC_STOP_POST)))
+ socket_run_next(s, success);
+ else {
+ /* No further commands for this step, so let's figure
+ * out what to do next */
- exec_context_free(&s->exec_context);
+ switch (s->state) {
+
+ case SOCKET_START_PRE:
+ if (success)
+ socket_enter_start_pre(s);
+ else
+ socket_enter_stop_pre(s, false);
+ break;
+
+ case SOCKET_START_POST:
+ if (success)
+ socket_set_state(s, SOCKET_LISTENING);
+ else
+ socket_enter_stop_pre(s, false);
+ break;
+
+ case SOCKET_STOP_PRE:
+ case SOCKET_STOP_PRE_SIGTERM:
+ case SOCKET_STOP_PRE_SIGKILL:
+ socket_enter_stop_post(s, success);
+ break;
+
+ case SOCKET_STOP_POST:
+ case SOCKET_STOP_POST_SIGTERM:
+ case SOCKET_STOP_POST_SIGKILL:
+ socket_enter_dead(s, success);
+ break;
+
+ default:
+ assert_not_reached("Uh, control process died at wrong time.");
+ }
+ }
+}
- for (c = 0; c < _SOCKET_EXEC_MAX; c++)
- exec_command_free_list(s->exec_command[c]);
+static void socket_timer_event(Name *n, int id, uint64_t elapsed) {
+ Socket *s = SOCKET(n);
- if (s->service)
- s->service->socket = NULL;
+ assert(s);
+ assert(elapsed == 1);
+
+ assert(s->timer_id == id);
+
+ switch (s->state) {
+
+ case SOCKET_START_PRE:
+ case SOCKET_START_POST:
+ log_warning("%s operation timed out. Stopping.", name_id(n));
+ socket_enter_stop_pre(s, false);
+ break;
+
+ case SOCKET_STOP_PRE:
+ log_warning("%s stopping timed out. Terminating.", name_id(n));
+ socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, false);
+ break;
+
+ case SOCKET_STOP_PRE_SIGTERM:
+ log_warning("%s stopping timed out. Killing.", name_id(n));
+ socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, false);
+ break;
+
+ case SOCKET_STOP_PRE_SIGKILL:
+ log_warning("%s still around after SIGKILL. Ignoring.", name_id(n));
+ socket_enter_stop_post(s, false);
+ break;
+
+ case SOCKET_STOP_POST:
+ log_warning("%s stopping timed out (2). Terminating.", name_id(n));
+ socket_enter_signal(s, SOCKET_STOP_POST_SIGTERM, false);
+ break;
+
+ case SOCKET_STOP_POST_SIGTERM:
+ log_warning("%s stopping timed out (2). Killing.", name_id(n));
+ socket_enter_signal(s, SOCKET_STOP_POST_SIGKILL, false);
+ break;
+
+ case SOCKET_STOP_POST_SIGKILL:
+ log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", name_id(n));
+ socket_enter_dead(s, false);
+ break;
+
+ default:
+ assert_not_reached("Timeout at wrong time.");
+ }
}
const NameVTable socket_vtable = {
.suffix = ".socket",
- .load = socket_load,
+ .init = socket_init,
+ .done = socket_done,
+
.dump = socket_dump,
.start = socket_start,
.stop = socket_stop,
- .reload = NULL,
.active_state = socket_active_state,
.fd_event = socket_fd_event,
-
- .free_hook = socket_free_hook
+ .sigchld_event = socket_sigchld_event,
+ .timer_event = socket_timer_event
};