X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=socket.c;h=755bc598da7e035b3a278525b543e717ff4caec4;hp=22658dcb009c3d273c418f35cfa67dcdf76c7c0c;hb=cd1a19e86dacc60930c2bedf02080026706c04b6;hpb=4139c1b2729f88991159b9affa2ebf3e4eb904a0 diff --git a/socket.c b/socket.c index 22658dcb0..755bc598d 100644 --- a/socket.c +++ b/socket.c @@ -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; @@ -129,9 +119,14 @@ static bool have_non_accept_socket(Socket *s) { if (!s->accept) return true; - LIST_FOREACH(port, p, s->ports) + LIST_FOREACH(port, p, s->ports) { + + if (p->type != SOCKET_SOCKET) + return true; + if (!socket_address_can_accept(&p->address)) return true; + } return false; } @@ -167,7 +162,7 @@ static int socket_load(Unit *u) { if ((r = unit_load_related_unit(u, ".service", (Unit**) &s->service))) return r; - if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service))) < 0) + if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service), true)) < 0) return r; } @@ -196,13 +191,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 +210,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), @@ -268,8 +256,8 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) { if (!s->exec_command[c]) continue; - fprintf(f, "%s→ %s:\n", - prefix, command_table[c]); + fprintf(f, "%s-> %s:\n", + prefix, socket_exec_command_to_string(c)); exec_command_dump_list(s->exec_command[c], f, prefix2); } @@ -307,7 +295,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 +310,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 +356,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; } @@ -457,8 +453,9 @@ static int socket_watch_fds(Socket *s) { if (p->fd < 0) continue; - p->fd_watch.data.socket_accept = + p->fd_watch.socket_accept = s->accept && + p->type == SOCKET_SOCKET && socket_address_can_accept(&p->address); if ((r = unit_watch_fd(UNIT(s), p->fd, EPOLLIN, &p->fd_watch)) < 0) @@ -490,8 +487,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 +501,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 +622,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; @@ -617,7 +667,7 @@ static void socket_enter_signal(Socket *s, SocketState state, bool success) { } } - if (sent) { + if (sent && s->control_pid > 0) { if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) goto fail; @@ -647,6 +697,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 +741,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 +765,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 +932,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 +1077,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) { @@ -902,7 +1094,7 @@ static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { goto fail; } - if (w->data.socket_accept) { + if (w->socket_accept) { for (;;) { if ((cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK)) < 0) { @@ -936,21 +1128,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 +1277,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,