X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Ffrontend%2Fsock.c;h=414c90775657294c0408ffb1e8c1eaea78ae8f0d;hb=40bc8e8e47d2e171fd0fafe1eeecd2a0241cd33c;hp=a389a142b94f8a1a609a02a2ae1773239dc0e624;hpb=635debdd1d9f4b4982cdff91fcb244b64ea341fc;p=sysdb.git diff --git a/src/frontend/sock.c b/src/frontend/sock.c index a389a14..414c907 100644 --- a/src/frontend/sock.c +++ b/src/frontend/sock.c @@ -92,6 +92,10 @@ struct sdb_fe_socket { size_t listeners_num; sdb_llist_t *open_connections; + + /* channel used for communication between main + * and connection handler threads */ + sdb_channel_t *chan; }; /* @@ -177,6 +181,7 @@ listener_destroy(listener_t *listener) if (listener->sock_fd >= 0) close(listener->sock_fd); + listener->sock_fd = -1; if (listener->address) free(listener->address); @@ -228,6 +233,48 @@ listener_create(sdb_fe_socket_t *sock, const char *address) return listener; } /* listener_create */ +static int +listener_listen(listener_t *listener) +{ + assert(listener); + + /* try to reopen */ + if (listener->sock_fd < 0) + if (listener_impls[listener->type].opener(listener)) + return -1; + assert(listener->sock_fd >= 0); + + if (listen(listener->sock_fd, /* backlog = */ 32)) { + char buf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to listen on socket %s: %s", + listener->address, sdb_strerror(errno, buf, sizeof(buf))); + return -1; + } + return 0; +} /* listener_listen */ + +static void +listener_close(listener_t *listener) +{ + assert(listener); + + if (listener->sock_fd < 0) + return; + + close(listener->sock_fd); + listener->sock_fd = -1; +} /* listener_close */ + +static void +socket_close(sdb_fe_socket_t *sock) +{ + size_t i; + + assert(sock); + for (i = 0; i < sock->listeners_num; ++i) + listener_close(sock->listeners + i); +} /* socket_close */ + /* * private data types */ @@ -288,6 +335,7 @@ connection_destroy(sdb_object_t *obj) assert(obj); conn = &CONN(obj)->conn; + sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection on fd=%i", conn->fd); close(conn->fd); conn->fd = -1; } /* connection_destroy */ @@ -302,12 +350,41 @@ static sdb_type_t connection_type = { * connection handler functions */ +/* returns negative value on error, 0 on EOF, number of packets else */ +static int +connection_read(int fd) +{ + int n = 0; + + while (42) { + int32_t cmd; + ssize_t status; + + errno = 0; + status = read(fd, &cmd, sizeof(cmd)); + if (status < 0) { + if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) + return n + 1; + return (int)status; + } + else if (! status) /* EOF */ + return 0; + + /* XXX */ + sdb_log(SDB_LOG_DEBUG, "frontend: read command %i from fd=%i", + cmd, fd); + ++n; + } + + return n + 1; +} /* connection_read */ + static void * connection_handler(void *data) { - sdb_channel_t *chan = data; + sdb_fe_socket_t *sock = data; - assert(chan); + assert(sock); while (42) { struct timespec timeout = { 0, 500000000 }; /* .5 seconds */ @@ -315,7 +392,8 @@ connection_handler(void *data) int status; errno = 0; - status = sdb_channel_select(chan, NULL, &conn, NULL, NULL, &timeout); + status = sdb_channel_select(sock->chan, /* read */ NULL, &conn, + /* write */ NULL, NULL, &timeout); if (status) { char buf[1024]; @@ -329,20 +407,32 @@ connection_handler(void *data) continue; } - /* XXX */ - sdb_log(SDB_LOG_INFO, "frontend: Data available on connection fd=%i\n", - conn->conn.fd); + status = connection_read(conn->conn.fd); + if (status <= 0) { + /* error or EOF -> close connection */ + sdb_object_deref(SDB_OBJ(conn)); + } + else { + if (sdb_llist_append(sock->open_connections, SDB_OBJ(conn))) { + sdb_log(SDB_LOG_ERR, "frontend: Failed to re-append " + "connection %s to list of open connections", + SDB_OBJ(conn)->name); + } + + /* pass ownership back to list; or destroy in case of an error */ + sdb_object_deref(SDB_OBJ(conn)); + } } return NULL; } /* connection_handler */ static int -accept_connection(sdb_fe_socket_t *sock, listener_t *listener) +connection_accept(sdb_fe_socket_t *sock, listener_t *listener) { sdb_object_t *obj; - /* the X's will be replaced with the accepted file descriptor - * when initializing the object */ + /* the placeholder will be replaced with the accepted file + * descriptor when initializing the object */ obj = sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER, connection_type, listener->sock_fd); if (! obj) @@ -359,7 +449,44 @@ accept_connection(sdb_fe_socket_t *sock, listener_t *listener) /* hand ownership over to the list */ sdb_object_deref(obj); return 0; -} /* accept_connection */ +} /* connection_accept */ + +static int +socket_handle_incoming(sdb_fe_socket_t *sock, + fd_set *ready, fd_set *exceptions) +{ + sdb_llist_iter_t *iter; + size_t i; + + for (i = 0; i < sock->listeners_num; ++i) { + listener_t *listener = sock->listeners + i; + if (FD_ISSET(listener->sock_fd, ready)) + if (connection_accept(sock, listener)) + continue; + } + + iter = sdb_llist_get_iter(sock->open_connections); + if (! iter) { + sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator " + "for open connections"); + return -1; + } + + while (sdb_llist_iter_has_next(iter)) { + sdb_object_t *obj = sdb_llist_iter_get_next(iter); + + if (FD_ISSET(CONN(obj)->conn.fd, exceptions)) + sdb_log(SDB_LOG_INFO, "Exception on fd %d", + CONN(obj)->conn.fd); + + if (FD_ISSET(CONN(obj)->conn.fd, ready)) { + sdb_llist_iter_remove_current(iter); + sdb_channel_write(sock->chan, &obj); + } + } + sdb_llist_iter_destroy(iter); + return 0; +} /* socket_handle_incoming */ /* * public API @@ -419,7 +546,6 @@ sdb_fe_sock_add_listener(sdb_fe_socket_t *sock, const char *address) int sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop) { - sdb_channel_t *chan; fd_set sockets; int max_listen_fd = 0; size_t i; @@ -427,18 +553,15 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop) /* XXX: make the number of threads configurable */ pthread_t handler_threads[5]; - if ((! sock) || (! sock->listeners_num) || (! loop)) + if ((! sock) || (! sock->listeners_num) || (! loop) || sock->chan) return -1; FD_ZERO(&sockets); - for (i = 0; i < sock->listeners_num; ++i) { listener_t *listener = sock->listeners + i; - if (listen(listener->sock_fd, /* backlog = */ 32)) { - char buf[1024]; - sdb_log(SDB_LOG_ERR, "frontend: Failed to listen on socket %s: %s", - listener->address, sdb_strerror(errno, buf, sizeof(buf))); + if (listener_listen(listener)) { + socket_close(sock); return -1; } @@ -447,37 +570,37 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop) max_listen_fd = listener->sock_fd; } - chan = sdb_channel_create(1024, sizeof(connection_obj_t *)); - if (! chan) + sock->chan = sdb_channel_create(1024, sizeof(connection_obj_t *)); + if (! sock->chan) { + socket_close(sock); return -1; + } memset(&handler_threads, 0, sizeof(handler_threads)); /* XXX: error handling */ for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i) pthread_create(&handler_threads[i], /* attr = */ NULL, - connection_handler, /* arg = */ chan); + connection_handler, /* arg = */ sock); while (loop->do_loop) { + struct timeval timeout = { 1, 0 }; /* one second */ + sdb_llist_iter_t *iter; + + int max_fd = max_listen_fd; fd_set ready; fd_set exceptions; - int max_fd; int n; - struct timeval timeout = { 1, 0 }; /* one second */ - sdb_llist_iter_t *iter; - FD_ZERO(&ready); FD_ZERO(&exceptions); ready = sockets; - max_fd = max_listen_fd; - iter = sdb_llist_get_iter(sock->open_connections); if (! iter) { sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator " "for open connections"); - return -1; + break; } while (sdb_llist_iter_has_next(iter)) { @@ -500,48 +623,27 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop) sdb_log(SDB_LOG_ERR, "frontend: Failed to monitor sockets: %s", sdb_strerror(errno, buf, sizeof(buf))); - return -1; + break; } - - if (! n) + else if (! n) continue; - for (i = 0; i < sock->listeners_num; ++i) { - listener_t *listener = sock->listeners + i; - if (FD_ISSET(listener->sock_fd, &ready)) - if (accept_connection(sock, listener)) - continue; - } - - iter = sdb_llist_get_iter(sock->open_connections); - if (! iter) { - sdb_log(SDB_LOG_ERR, "frontend: Failed to acquire iterator " - "for open connections"); - return -1; - } - - while (sdb_llist_iter_has_next(iter)) { - sdb_object_t *obj = sdb_llist_iter_get_next(iter); - - if (FD_ISSET(CONN(obj)->conn.fd, &exceptions)) - sdb_log(SDB_LOG_INFO, "Exception on fd %d", - CONN(obj)->conn.fd); - - if (FD_ISSET(CONN(obj)->conn.fd, &ready)) { - sdb_log(SDB_LOG_INFO, "Data on fd %d", CONN(obj)->conn.fd); - sdb_llist_iter_remove_current(iter); - sdb_channel_write(chan, &obj); - } - } - sdb_llist_iter_destroy(iter); + /* handle new and open connections */ + if (socket_handle_incoming(sock, &ready, &exceptions)) + break; } + socket_close(sock); + sdb_log(SDB_LOG_INFO, "frontend: Waiting for connection handler threads " "to terminate"); - if (! sdb_channel_shutdown(chan)) + if (! sdb_channel_shutdown(sock->chan)) for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i) pthread_join(handler_threads[i], NULL); /* else: we tried our best; let the operating system clean up */ + + sdb_channel_destroy(sock->chan); + sock->chan = NULL; return 0; } /* sdb_fe_sock_listen_and_server */