diff options
Diffstat (limited to 'socket.c')
-rw-r--r-- | socket.c | 331 |
1 files changed, 276 insertions, 55 deletions
@@ -52,20 +52,22 @@ static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = { [SOCKET_MAINTAINANCE] = UNIT_INACTIVE, }; -static const char* const state_string_table[_SOCKET_STATE_MAX] = { - [SOCKET_DEAD] = "dead", - [SOCKET_START_PRE] = "start-pre", - [SOCKET_START_POST] = "start-post", - [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_FINAL_SIGTERM] = "final-sigterm", - [SOCKET_FINAL_SIGKILL] = "final-sigkill", - [SOCKET_MAINTAINANCE] = "maintainance" -}; +static void socket_init(Unit *u) { + Socket *s = SOCKET(u); + + assert(u); + assert(u->meta.load_state == UNIT_STUB); + + s->timer_watch.type = WATCH_INVALID; + s->backlog = SOMAXCONN; + s->timeout_usec = DEFAULT_TIMEOUT_USEC; + s->directory_mode = 0755; + s->socket_mode = 0666; + + exec_context_init(&s->exec_context); + + s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; +} static void socket_unwatch_control_pid(Socket *s) { assert(s); @@ -86,8 +88,11 @@ static void socket_done(Unit *u) { while ((p = s->ports)) { LIST_REMOVE(SocketPort, port, s->ports, p); - if (p->fd >= 0) - close_nointr(p->fd); + if (p->fd >= 0) { + unit_unwatch_fd(UNIT(s), &p->fd_watch); + close_nointr_nofail(p->fd); + } + free(p->path); free(p); } @@ -106,21 +111,6 @@ static void socket_done(Unit *u) { unit_unwatch_timer(u, &s->timer_watch); } -static void socket_init(Unit *u) { - Socket *s = SOCKET(u); - - assert(u); - assert(u->meta.load_state == UNIT_STUB); - - s->timer_watch.type = WATCH_INVALID; - s->backlog = SOMAXCONN; - s->timeout_usec = DEFAULT_TIMEOUT_USEC; - s->directory_mode = 0755; - s->socket_mode = 0666; - - exec_context_init(&s->exec_context); -} - static bool have_non_accept_socket(Socket *s) { SocketPort *p; @@ -196,13 +186,6 @@ static const char* listen_lookup(int type) { static void socket_dump(Unit *u, FILE *f, const char *prefix) { - static const char* const command_table[_SOCKET_EXEC_COMMAND_MAX] = { - [SOCKET_EXEC_START_PRE] = "StartPre", - [SOCKET_EXEC_START_POST] = "StartPost", - [SOCKET_EXEC_STOP_PRE] = "StopPre", - [SOCKET_EXEC_STOP_POST] = "StopPost" - }; - SocketExecCommand c; Socket *s = SOCKET(u); SocketPort *p; @@ -222,7 +205,7 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) { "%sKillMode: %s\n" "%sSocketMode: %04o\n" "%sDirectoryMode: %04o\n", - prefix, state_string_table[s->state], + prefix, socket_state_to_string(s->state), prefix, yes_no(s->bind_ipv6_only), prefix, s->backlog, prefix, kill_mode_to_string(s->kill_mode), @@ -269,7 +252,7 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) { continue; fprintf(f, "%sā %s:\n", - prefix, command_table[c]); + prefix, socket_exec_command_to_string(c)); exec_command_dump_list(s->exec_command[c], f, prefix2); } @@ -307,7 +290,7 @@ static int instance_from_socket(int fd, unsigned nr, char **instance) { b = ntohl(remote.in.sin_addr.s_addr); if (asprintf(&r, - "%u-%u.%u.%u.%u-%u-%u.%u.%u.%u-%u", + "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u", nr, a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF, ntohs(local.in.sin_port), @@ -322,7 +305,7 @@ static int instance_from_socket(int fd, unsigned nr, char **instance) { char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN]; if (asprintf(&r, - "%u-%s-%u-%s-%u", + "%u-%s:%u-%s:%u", nr, inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)), ntohs(local.in6.sin6_port), @@ -368,7 +351,15 @@ static void socket_close_fds(Socket *s) { continue; unit_unwatch_fd(UNIT(s), &p->fd_watch); - assert_se(close_nointr(p->fd) >= 0); + close_nointr_nofail(p->fd); + + /* One little note: we should never delete any sockets + * in the file system here! After all some other + * process we spawned might still have a reference of + * this fd and wants to continue to use it. Therefore + * we delete sockets in the file system before we + * create a new one, not after we stopped using + * one! */ p->fd = -1; } @@ -490,8 +481,12 @@ static void socket_set_state(Socket *s, SocketState state) { unit_unwatch_timer(UNIT(s), &s->timer_watch); socket_unwatch_control_pid(s); s->control_command = NULL; + s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; } + if (state != SOCKET_LISTENING) + socket_unwatch_fds(s); + if (state != SOCKET_START_POST && state != SOCKET_LISTENING && state != SOCKET_RUNNING && @@ -500,15 +495,62 @@ static void socket_set_state(Socket *s, SocketState state) { 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", s->meta.id, state_string_table[old_state], state_string_table[state]); + log_debug("%s changed %s ā %s", + s->meta.id, + socket_state_to_string(old_state), + socket_state_to_string(state)); unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]); } +static int socket_coldplug(Unit *u) { + Socket *s = SOCKET(u); + int r; + + assert(s); + assert(s->state == SOCKET_DEAD); + + if (s->deserialized_state != s->state) { + + if (s->deserialized_state == SOCKET_START_PRE || + s->deserialized_state == SOCKET_START_POST || + s->deserialized_state == SOCKET_STOP_PRE || + s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || + s->deserialized_state == SOCKET_STOP_PRE_SIGKILL || + s->deserialized_state == SOCKET_STOP_POST || + s->deserialized_state == SOCKET_FINAL_SIGTERM || + s->deserialized_state == SOCKET_FINAL_SIGKILL) { + + if (s->control_pid <= 0) + return -EBADMSG; + + if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0) + return r; + + if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) + return r; + } + + if (s->deserialized_state == SOCKET_START_POST || + s->deserialized_state == SOCKET_LISTENING || + s->deserialized_state == SOCKET_RUNNING || + s->deserialized_state == SOCKET_STOP_PRE || + s->deserialized_state == SOCKET_STOP_PRE_SIGTERM || + s->deserialized_state == SOCKET_STOP_PRE_SIGKILL) + if ((r = socket_open_fds(s)) < 0) + return r; + + if (s->deserialized_state == SOCKET_LISTENING) + if ((r = socket_watch_fds(s)) < 0) + return r; + + socket_set_state(s, s->deserialized_state); + } + + return 0; +} + static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) { pid_t pid; int r; @@ -574,6 +616,8 @@ static void socket_enter_stop_post(Socket *s, bool success) { socket_unwatch_control_pid(s); + s->control_command_id = SOCKET_EXEC_STOP_POST; + 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; @@ -647,6 +691,8 @@ static void socket_enter_stop_pre(Socket *s, bool success) { socket_unwatch_control_pid(s); + s->control_command_id = SOCKET_EXEC_STOP_PRE; + 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; @@ -689,6 +735,8 @@ static void socket_enter_start_post(Socket *s) { socket_unwatch_control_pid(s); + s->control_command_id = SOCKET_EXEC_START_POST; + 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", s->meta.id, strerror(-r)); @@ -711,6 +759,8 @@ static void socket_enter_start_pre(Socket *s) { socket_unwatch_control_pid(s); + s->control_command_id = SOCKET_EXEC_START_PRE; + if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) { if ((r = socket_spawn(s, s->control_command, &s->control_pid)) < 0) goto fail; @@ -876,6 +926,142 @@ static int socket_stop(Unit *u) { return 0; } +static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { + Socket *s = SOCKET(u); + SocketPort *p; + int r; + + assert(u); + assert(f); + assert(fds); + + unit_serialize_item(u, f, "state", socket_state_to_string(s->state)); + unit_serialize_item(u, f, "failure", yes_no(s->failure)); + unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted); + + if (s->control_pid > 0) + unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) s->control_pid); + + if (s->control_command_id >= 0) + unit_serialize_item(u, f, "control-command", socket_exec_command_to_string(s->control_command_id)); + + LIST_FOREACH(port, p, s->ports) { + int copy; + + if (p->fd < 0) + continue; + + if ((copy = fdset_put_dup(fds, p->fd)) < 0) + return copy; + + if (p->type == SOCKET_SOCKET) { + char *t; + + if ((r = socket_address_print(&p->address, &t)) < 0) + return r; + + unit_serialize_item_format(u, f, "socket", "%i %s", copy, t); + free(t); + } else { + assert(p->type == SOCKET_FIFO); + unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path); + } + } + + return 0; +} + +static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { + Socket *s = SOCKET(u); + int r; + + assert(u); + assert(key); + assert(value); + assert(fds); + + if (streq(key, "state")) { + SocketState state; + + if ((state = socket_state_from_string(value)) < 0) + log_debug("Failed to parse state value %s", value); + else + s->deserialized_state = state; + } else if (streq(key, "failure")) { + int b; + + if ((b = parse_boolean(value)) < 0) + log_debug("Failed to parse failure value %s", value); + else + s->failure = b || s->failure; + + } else if (streq(key, "n-accepted")) { + unsigned k; + + if ((r = safe_atou(value, &k)) < 0) + log_debug("Failed to parse n-accepted value %s", value); + else + s->n_accepted += k; + } else if (streq(key, "control-pid")) { + unsigned pid; + + if ((r = safe_atou(value, &pid)) < 0 || pid <= 0) + log_debug("Failed to parse control-pid value %s", value); + else + s->control_pid = (pid_t) pid; + } else if (streq(key, "control-command")) { + SocketExecCommand id; + + if ((id = socket_exec_command_from_string(value)) < 0) + log_debug("Failed to parse exec-command value %s", value); + else { + s->control_command_id = id; + s->control_command = s->exec_command[id]; + } + } else if (streq(key, "fifo")) { + int fd, skip = 0; + SocketPort *p; + + if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) + log_debug("Failed to parse fifo value %s", value); + else { + + LIST_FOREACH(port, p, s->ports) + if (streq(p->path, value+skip)) + break; + + if (p) { + if (p->fd >= 0) + close_nointr_nofail(p->fd); + p->fd = fdset_remove(fds, fd); + } + } + + } else if (streq(key, "socket")) { + int fd, skip = 0; + SocketPort *p; + + if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd)) + log_debug("Failed to parse socket value %s", value); + else { + + LIST_FOREACH(port, p, s->ports) + if (socket_address_is(&p->address, value+skip)) + break; + + if (p) { + if (p->fd >= 0) + close_nointr_nofail(p->fd); + p->fd = fdset_remove(fds, fd); + } + } + + } else + log_debug("Unknown serialization key '%s'", key); + + return 0; +} + static UnitActiveState socket_active_state(Unit *u) { assert(u); @@ -885,7 +1071,7 @@ static UnitActiveState socket_active_state(Unit *u) { static const char *socket_sub_state_to_string(Unit *u) { assert(u); - return state_string_table[SOCKET(u)->state]; + return socket_state_to_string(SOCKET(u)->state); } static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { @@ -918,6 +1104,7 @@ static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { } } + log_debug("cfd=%i", cfd); socket_enter_running(s, cfd); return; @@ -936,21 +1123,24 @@ static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) { s->failure = s->failure || !success; assert(s->control_pid == pid); - assert(s->control_command); - - exec_status_fill(&s->control_command->exec_status, pid, code, status); s->control_pid = 0; + if (s->control_command) + exec_status_fill(&s->control_command->exec_status, pid, code, status); + log_debug("%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status); - if (s->control_command->command_next && success) { - log_debug("%s running next command for state %s", u->meta.id, state_string_table[s->state]); + if (s->control_command && s->control_command->command_next && success) { + log_debug("%s running next command for state %s", u->meta.id, socket_state_to_string(s->state)); socket_run_next(s, success); } else { + s->control_command = NULL; + s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; + /* No further commands for this step, so let's figure * out what to do next */ - log_debug("%s got final SIGCHLD for state %s", u->meta.id, state_string_table[s->state]); + log_debug("%s got final SIGCHLD for state %s", u->meta.id, socket_state_to_string(s->state)); switch (s->state) { @@ -1082,18 +1272,49 @@ void socket_notify_service_dead(Socket *s) { } } +static const char* const socket_state_table[_SOCKET_STATE_MAX] = { + [SOCKET_DEAD] = "dead", + [SOCKET_START_PRE] = "start-pre", + [SOCKET_START_POST] = "start-post", + [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_FINAL_SIGTERM] = "final-sigterm", + [SOCKET_FINAL_SIGKILL] = "final-sigkill", + [SOCKET_MAINTAINANCE] = "maintainance" +}; + +DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState); + +static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = { + [SOCKET_EXEC_START_PRE] = "StartPre", + [SOCKET_EXEC_START_POST] = "StartPost", + [SOCKET_EXEC_STOP_PRE] = "StopPre", + [SOCKET_EXEC_STOP_POST] = "StopPost" +}; + +DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand); + const UnitVTable socket_vtable = { .suffix = ".socket", .init = socket_init, - .load = socket_load, .done = socket_done, + .load = socket_load, + + .coldplug = socket_coldplug, .dump = socket_dump, .start = socket_start, .stop = socket_stop, + .serialize = socket_serialize, + .deserialize_item = socket_deserialize_item, + .active_state = socket_active_state, .sub_state_to_string = socket_sub_state_to_string, |